This is the question I was working on. I have created a function to convert binary number to decimal but what I couldn't figure out was how to get values while the number after Enter change
Enter 4 position value
Enter 3 position value
Enter 2 position value
Enter 1 position value
This is the code which I tried but apparently prompt inside input does not work like it does in print function. The numbers after Enter change depending on how much the user wants to enter.
a=int(input("Enter Number of digits : "))
while a<0:
x=int(input("Enter ",a, " position value : "))
CodePudding user response:
input()
only expects a single argument; use string formatting to pass it a single string with your position value in it
One of these is likely what you're after (functionally identical, but you may have some preference)
int(input(f"Enter {a} position value: "))
int(input("Enter {} position value: ".format(a)))
Multiple arguments to input()
will raise TypeError
!
>>> input("foo", "bar")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: input expected at most 1 argument, got 2
CodePudding user response:
You could print before the input
b = 0
a = int(input("Enter Number of digits: "))
# digits = [0 for _ in range(a)]
b |= int(input("Enter MSB Value")) # or digits[a-1] = int(input())
a -= 1
while a > 0:
print("Enter " , a, " position value : ")
x = int(input()) # or digits[a] = int(input())
b |= (x << a) # just an example
a -= 1
But if you want to put the input on the same line, then you need to format or concatenate the string to the input function, which is not what commas do
Regarding the storing of binary numbers, you should ideally be using bit-shift logic (as shown above) for that since you aren't actually entering decimal values, only individual bits. (Logic above not tested for your problem, btw)
And you don't need to write your own function to convert