This question already has answers here:
Can't send input to running program in Sublime Text
(5 answers)
Closed 2 years ago.
I have the following function:
years=2
double_rate=(years*12)//3
number_of_rabbits=11
answer=number_of_rabbits*double_rate
print(answer)
I want to generalize the code by being able to input years and number_of_rabbits variables
years=input("select number of years: ")
print(years)
double_rate=(years*12)//3
number_of_rabbits=input("select number of rabbits: ")
print(number_of_rabbits)
answer=number_of_rabbits*double_rate
print(answer)
However the editor (sublime text) only prompts me for the first input variable. I am not able set the second one, "select number of rabbits", nor does it print the new answer
Does anyone know why this is the case?
The number of years you enter is saved as a string, not an int (or float). So when you try to calculate double_rate, you're multiplying a string by 12 (which is fine) and then floor dividing the result by 3, which doesn't work.
Try years = int(input("Select number of years: ")) instead.
Related
This question already has answers here:
How can I read inputs as numbers?
(10 answers)
Closed 1 year ago.
a=input("Enter First Number ")
b=input("Enter second number ")
c=a+b
print("The Sum of two numbers are ",c)
"Why This Program not printing Sum of tho number?s?""
You need to cast the numbers to int. When you take an input, it is always a string. To cast a string to int, you can do int(a)+int(b)
This question already has answers here:
Why does multiplication repeats the number several times? [closed]
(7 answers)
Closed 1 year ago.
I’ve written a piece of code that instead of print the product, prints a number a certain number of times. Whats wrong with it?
twelve = 12
name = input("What is your name? \nAnswer: ")
print("Cool name!")
nums = input("\n\nHow much pocket money did you receive last month?\nAnswer: ")
total = nums * twelve
print("\n\nI think you get ", total + " pounds in pocket money per year! Nice!")
The reason is that your nums variable is a string, which is the default with all Python inputs. Try converting it to int:
nums = int(input(...))
Or float if you are inputting a floating point number.
This question already has answers here:
How do you input integers using input in Python [duplicate]
(3 answers)
Closed 2 years ago.
whats my mistake?
it doesn't show you are right
print("what's the correct number?")
print("1/2/3/4/5/6/7/8/9")
correctNumber = 6
inputNumber = input()
if inputNumber == correctNumber:
print("you are right")
You can try
correctNumber = 6
inputNumber = int(input("what's the correct number? 1/2/3/4/5/6/7/8/9"))
if inputNumber == correctNumber:
print("you are right")
The problem was that input is store in a variable as a string and you need to cast it to int to compare it with int since correctNumber = 6 is int.
By the way, the input method expects a string to print the user before his input so you don't need to use print statements to notify the user about the expected input.
When using input(), you get a string that you compare int () and str (). to get a number in water you need int(input())
This question already has answers here:
How does my input not equal the answer?
(2 answers)
How can I read inputs as numbers?
(10 answers)
Closed 3 years ago.
any reason why this doesn't work?
import random
answer = random.randint(2, 4)
print(answer)
txt = input("what number")
if answer == txt:
print("correct")
every time I enter the correct answer, it just comes back empty (or as an else statement if I put it)
I had this working last night although now I cannot work out why it won't, PS. i've just started to learn python this week so please go easy on me
you can not compare string with integer.
import random
answer = random.randint(2, 4)
print(answer)
txt = int(input("what number"))
if answer == txt:
print("correct")
This question already has answers here:
How can I read inputs as numbers?
(10 answers)
Closed 3 years ago.
I'm beginner in python but I don't understand a thing.
This is the Code:
a = input("Insert first number ")
b = input("Insert second number ")
c = input("Insert third number ")
print("Max number is", max(a, b, c))
For example, I write at prompt:
Insert first number 12
Insert second number 34
Insert third number 100
Max number is 34
I don't understand! Please answer me!
Input returns string and strings are compared lexicographically. You should cast all input results to int, to get numbers.