User input with "SublimeRepl" for a python script - python-3.x

I am running a simple python script and using Sublime Text 3 as IDE using SublimeRepl for user input.
The below script always prints the else part even though I give a number less than 5.
num = input ("Enter the number bw 1-5")
if num in range(1,5):
print("Valid option")
else:
print("Invalid")

This has worked. Input is always a String, so we need to typecast to Int.
num = int(input ("Enter the number bw 1-5"))
if num in range(1,5):print("Valid option")
else:print("Invalid")

Related

Really basic python3 user input type

I want to ask a user for a number(among other things) and if they input anything other than an int, it should tell them to try again.
I'm still getting use to python syntax, what's the best way to do this?
excerpt below of what I tried:
try:
num = int(i)
except ValueError:
while type(num) != int:
num = input("Please input an actual number or q to quit: ")
while True:
num = input("Input a number. ")
if num.isdigit()==False:
print("Try again.")
else:
break
This should work unless if there's a negative value entered in which case you need to make a check if the first character is a - sign.

Input within if statement python3

This program works fine in python2, but prints error after inputing '1' at the prompt when running it in python3. I'm wondering if there is still a way to accept user input within an if statement or if I'd have to go about this differently. My program below:
#!/usr/bin/python3
select_one = input('Please enter a number from the menu below:
\n1.Convert Celcius to Farenheit \n2. Convert Farenheit to Celcius\n>\t')
if select_one == 1:
c = input('Please enter the temperature\n>\t')
f = c*(9.0/5.0)+32
print(f, 'F', sep='')
elif select_one == 2:
f = input('Please enter the temperature\n>\t')
c = f*(5.0/9.0)-32
print(c, 'C', sep='')
else:
print('Error')
By default the input() takes value as string in python 3.. so you might want to change input as
select_one =int( input('Please enter a number from the menu below: \n1.Convert Celcius to Farenheit \n2. Convert Farenheit to Celcius\n>\t'))
The other answers will get you partway, but a nicer way to handle input in your script would be something like this:
def input_type(prompt, outputType):
while TrueL
try:
return outputType(input(prompt))
except ValueError:
print("Invalid input. Required type is {}".format(outputType.__name__))
You can then replace your first call to input with input_type(<your prompt>, int) and the second two with input_type(<your prompt>, float) without changing the rest of your code.
As a side note - your conversion from fahrenheit to celsius isn't quite right. You might want to test it by converting from celsius to fahrenheit and back again.
You are accidentally relying on the fact that in Python 2, input() would parse the input, not just return it as a string. So "1" (str) becomes 1 (int). But in Python 3, input() does what raw_input() used to do, so the returned value is always a string.
To fix it, change this:
if select_one == 1:
To this:
if select_one == "1":

Python 3.6 Input output with variables

I have a question for python 3.6. I am trying to make an input-output system for a raspberry pi. I am fairly new to python and haven't found anything on this topic.
So far I have this:
j = input ("type a number")
n = input ("type a number")
j*n
The inputs work then the multiplication doesn't. Please help!
a = int(input ("type a number: "))
b = int(input ("type a number: "))
print ("a*b = ", a*b)
In the first example, input was not defined. It was just printing the input value. When input value was defined as the integer(int), it performed arithmetic operation and gave desired value.

Python 3.5.1 Introduction to Python 2.1 Mark Clarkson - While Loop Issue

I'm working my way through this set of tutorials. In section 3.2a - While Loops the following code is supposed to loop until the user enters the target number (7) then display a congratulations message however regardless of what number is entered Python either gives a right answer or a wrong answer, even 7 will sometimes flag a wrong answer. I know there are other ways to perform this sort of task but I would like to get the code from the tutorial working.
targetNumber = 7
guess = input("Guess a number between 1 and 10 ")
while guess != targetNumber:
print("Wrong, try again ")
guess = input("Guess a number between 1 and 10 ")
print("Congratulations - that's right!")
You should convert the target numger to an string before comparison. Also, you should exclude the congratulations message from the loop. I would suggest :
targetNumber = str(7)
guess = input("Guess a number between 1 and 10 ")
while guess != targetNumber:
print("Wrong, try again ")
guess = input("Guess a number between 1 and 10 ")
print("Congratulations - that's right!")
The detail is that input returns a string and if you compare a string to an integer, it will always return false.
Python's input function (or raw_input in Python 2.x) returns a string entered by the user. targetNumber, on the other hand, is an integer. In the Python Interpreter, try:
>>> 7 == "7"
False
You need to cast the user's input to an integer first.
try:
guess = int(input("Please enter a number: "))
except ValueError:
print("That is not a valid number!")

When I enter a negative number it prints 'sorry' not the absolute number, what a I doing wrong?

def function_1():
var_1 = input('''Enter any number''')
if type(var_1) == int:
print (abs(var_1))
else:
print ('''sorry''')
function_1()
When I enter in a negative number it prints the else statement 'sorry' instead of giving me the absolute number
In Python 3, input() returns always a string.
In Python 2 input() evaluates the text as a Python expression, and raw_input() returns a string, this behavior is error prone as the most intuitive input mode can introduce different errors and unexpected behaviors. For that reason in Python 3 input() behaves like raw_input() does in Python 2.
You can convert the input to int with:
var_1 = int(input())

Resources