how to ask for a numerical input n then use it to perform a numerical operation | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

how to ask for a numerical input n then use it to perform a numerical operation

I want to perform a numerical operation by using the input... I tried it but a error comes... saying it is not converted into int... it's a string

22nd Jun 2016, 3:49 PM
Satyam Pandey
3 Answers
0
I think I understand what you're saying. Try putting 'int' before the input code. Example: user_input = int(input('Enter number: ')) This should only allow integers to be inputted or else an error will occur. Side note: You may also use 'float' in the place of 'int' to accept decimal places as input (ex. 3.916).
22nd Jun 2016, 4:31 PM
Dean
0
thanks dean
22nd Jun 2016, 4:44 PM
Satyam Pandey
0
import sys n = int(input("Please enter how many bottles of beer to start with: ")) if n <= 0: print ("No bottles of beer") if sys.argv[1:]: n = int (sys.argv[1]) def bottle (n): if n == 0: return "no more bottles of beer" if n == 1: return "one bottle of beer" return are (n) + "bottles of beer" for i in range (n, 0, -1): print (bottle (i), "on the wall,") print (bottle (i) + ".") print ("Take one down, pass it around,") print (bottle (i-1), "on the wall.") I just used (i) but if (i) is replaced with any value such as (n) then it will flow. You can play with this code.
10th Aug 2016, 3:01 AM
Frank