How to make inputs not strings (python) [duplicate] - python

This question already has answers here:
How can I read inputs as numbers?
(10 answers)
Closed 3 months ago.
I am making a quick problem solver, it takes the speed (input) and time (also input) than multiplies them to get the distance... the problem is that python thinks the inputs are strings, how do i make them numbers???
my code so far is:
py
import random
time = input("What is the time it took? (no label :: ")
speed = input("What was the speed?(no label :: ")
s = speed
t = time
distance = s * t
print(time)
print(speed)
print(distance)

You should be able to simply cast the String input to an Integer. Doing as follows will give you the desired output.
s = int(speed)
t = int(time)

Related

Basic math in python [duplicate]

This question already has answers here:
How can I read inputs as numbers?
(10 answers)
Closed 1 year ago.
The code below is giving me a return of 25 with inputs of 3 & 4. Obviously it should be 7. This is a problem for school and I can't edit the first 3 lines or the last one. What am I missing here?
total_owls = 0
num_owls_A = input()
num_owls_B = input()
num_owls_A = int(input())
num_owls_B = int(input())
total_owls = (num_owls_A + num_owls_B)
print('Number of owls:', total_owls)
input() returns input value as a string. So, you are basically concatenating strings not integers.
If you want to add them as numbers you need to convert them to numbers first like below
num_owls_A = int(input())
num_owls_B = int(input())
Again, this will create an error, if you input a non-numerical value, so you need to handle the exceptions in such case.

Program printing a number a certain times instead of multiplying [duplicate]

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.

input() function not working in Sublime text [duplicate]

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.

really basic newbie python [duplicate]

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")

Python: asking user for input with prior input in the prompt? [duplicate]

This question already has answers here:
Is there a Python equivalent to Ruby's string interpolation?
(9 answers)
Closed 5 years ago.
I'm using Python, and I want to ask a user for input to define a variable, then ask for another input using what they inputted into the first question as part of the second question.
That might be confusing. I mean like this:
hometeam = str(input("Home team?"))
ppg = input(hometeam "Average Points per Game?")
print(ppg)
For example, if the user inputs "Cowboys" for hometeam, I'd like the program to ask "Cowboys Average Points per Game?" for the second question.
I'm getting a syntax error with a carrot pointing at the last quotation mark.
You saved that input into the variable "hometeam", so you can do something like this using "+":
ppg = input(hometeam + " Average Points per Game?")
hometeam = str(input("Home team?"))
ppg = input("{0} Average Points per Game?".format(hometeam))
print(ppg)

Categories