This question already has answers here:
What's the difference between `raw_input()` and `input()` in Python 3? [duplicate]
(6 answers)
How can I read inputs as numbers?
(10 answers)
Closed 6 years ago.
I have this line of code:
incoming = input("Type in 1 or 2")
if incoming == 1:
print ("you entered 1")
elif incoming == 2:
print ("you entered 2")
this worked perfectly fine when I used python 2... on my mac, but on windows with python 3, not so well.
Can anybody explain this to me?
Python 3.x doesn't evaluate and convert data types the way Python 2.x did. So, you are going to have to explicitly convert your user's input to an integer like this:
incoming = int(input("Type 1 or 2: "))
Related
This question already has answers here:
How can I check if a string represents an int, without using try/except?
(23 answers)
Asking the user for input until they give a valid response
(22 answers)
Closed 11 months ago.
I want to give a print message if the user inputs a string without using the try-exception method but with the if-else method.
The code is:
num = int(input("Enter the number = "))
for b in range(1,11):
print(num,"",b,"=",numb)
This question already has answers here:
How to read multiple lines of raw input?
(16 answers)
How to get multiline input from the user [duplicate]
(5 answers)
Closed 2 years ago.
If I have a large multiline input and I run this line:
story = input()
it reads it as two inputs and my program doesn't process it correctly. I found a fix on stackoverflow that I modified to look like this:
story = []
while True:
try:
line = input()
except EOFError:
break
story.append(line)
story = ' '.join(story)
But I would need to use ctrl+D to induce an EOFError to stop the input. Is there a more intuitive way on Python to copy paste a large multiline input?
This question already has answers here:
Identifying the data type of an input
(4 answers)
Closed 2 years ago.
I'm unable to compare users input as str or int in following code..
A=input(" ")
if type(A)=str:
Print("this is string")
elif type(A)=int:
Print("this is integer")
So please tell me how to deal with this type of problem
use string class isdigit() method to check user input is number or string.
This question already has answers here:
Getting user input [duplicate]
(5 answers)
Closed 5 years ago.
For a challenge I am tasked with creating a unit converter that can change the units. I chose degrees Celsius to Fahrenheit. I am quite new to Python. My problem is that I ask a question on the code e.g.
print("Enter Value: ")
How do I make it so that the value that a user enters becomes the variable f for Fahrenheit which can then be changed to Celsius so I can do this..
print((f - 32) / 1.8)
Can anyone help and explain it in a way a beginner can understand?
Assuming you're using Python3, what you need is:
temp=input("Temperature please?")
print((int(temp)-32)/1.8)
Also, please look up the docs Jacek linked to so that you understand what's really going on here.
Use input() function:
Input and Output Docs
temp = 0
# while loop
# wait until user set a input
while not temp:
# default type in input is "str"
user_input = input("Enter Value: ")
if user_input.isdigit():
temp = user_input
# we know every char is digit
print (((int(temp)-32)/1.8))
This question already has answers here:
How can I read inputs as numbers?
(10 answers)
Closed 7 years ago.
Could someone please help me to understand why the program doesn't output 'Well done!' after I input 1? Thank you.
while True:
o=input("Enter 1:")
if o==1:
break
print("Well done!")
It looks like you're using python3.x.
On python3.x, input always returns an instance of str, so o will never equal the 1 since you're comparing different types (str and int).