0

enter image description here
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 : "))

2 Answers2

2

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
ti7
  • 16,375
  • 6
  • 40
  • 68
  • Thanks for the help. Btw does the f after bracket helps it pass as a single string? – Vidit Agarwal Nov 17 '21 at 18:59
  • Anytime! The `f` is actually part of the string `f""` and makes it an "f-string" .. this enables you to use `{}` to format directly into the string https://docs.python.org/3/tutorial/inputoutput.html#formatted-string-literals – ti7 Nov 17 '21 at 19:01
  • Thanks this was new for me as I have only learnt the basics till now. – Vidit Agarwal Nov 17 '21 at 19:10
  • that's why we're all here! https://xkcd.com/1053/ – ti7 Nov 17 '21 at 19:11
0

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

Python: Binary To Decimal Conversion

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
  • setting `end=""` in the print args could keep the `input()` on the same line! but just coercing the line to a single value and passing it as the single argument to `input()` is probably much better (even if this was with something wild like [`contextlib.redirect_stdout()`](https://docs.python.org/3/library/contextlib.html#contextlib.redirect_stdout)) – ti7 Nov 17 '21 at 18:53
  • I would use int casting but here we have been specified to write a program so it is what it is. But thanks will remember this when I have to do this any other time. – Vidit Agarwal Nov 17 '21 at 18:55
  • 1
    @ViditAgarwal The problem you've asked about has nothing to do with "int casting". Plus, you're required to use "int casting" to get input to be numbers, anyway – OneCricketeer Nov 17 '21 at 18:56
  • Using print before input is also a great idea. Thanks for the tip. – Vidit Agarwal Nov 17 '21 at 18:56