Only allowing Integer input in python 3.3.2 [duplicate] - python

This question already has answers here:
Accepting only numbers as input in Python
(2 answers)
Closed 7 years ago.
Hi i am trying to make the program only accept the numbers 0, 4, 6, and 12, and not allow anything else to be inputted. So far i have been successful in only allowing certain integers to be entered, however i am having trouble with not allowing any letters to be entered. When a letter is entered, the entire program crashes. Please could you help me only allow integers to be entered? Thank you.
My code is below:
from random import randint
def simul():
dice = int(input("What sided dice would you like to roll? 4, 6 or 12? 0 to not roll:"))
if dice != 4 and dice!=6 and dice!=12 and dice!=0:
print('You must either enter 4, 6, or 12')
simul()
elif dice==0:
exit()
else:
while dice !=0 and dice==4 or dice==6 or dice ==12 :
print (randint(1,dice))
dice = int(input("What sided dice would you like to roll? 4, 6 or 12? press 0 to stop."))
simul()

A couple of things you could look for in your code:
using try/catch is the recommended way to test input for many reasons including knowing the exact cause of the error
you can reduce some of your ifs and elses by thinking a little more about how they are nested
having the function call itself and using a while loop isn't the best way, use one or the other
in your case, you don't really need to allow only integer input, what you're looking for is to only allow a 0, 4, 6, or 12, which you do with the if statement
from random import randint
def simul():
while True:
try:
dice = int(input("What sided dice would you like to" \
" roll? 4, 6 or 12? 0 to not roll: "))
if dice not in (4, 6, 12, 0):
raise ValueError()
break # valid value, exit the fail loop
except ValueError:
print("You must enter either 4, 6, 12, or 0")
if dice == 0:
return 0
print(randint(1, dice))
return dice
if __name__ == '__main__':
while simul() != 0:
pass

I would encapsulate the "constrained input" functionality into a separate, reusable function:
def constrained_int_input(prompt, accepted, toexit=0):
msg = '%s? Choose %s (or %s to exit)' % (
prompt, ', '.join(str(x) for x in sorted(accepted)), toexit)
while True:
raw = raw_input(msg)
try:
entered = int(raw)
except ValueError:
print('Enter a number, not %r' % raw)
continued
if entered == toexit or entered in accepted:
return entered
print('Invalid number: %r -- please enter a valid one' % entered)
Now you can call e.g
dice = constrained_int_input('What sided dice would you like to roll', (4,6,12))
whenever required and be sure that dice will end up with one of the accepted integers, including possibly the to-exit value of 0.

put it in a try catch block like so:
try:
choice = int(raw_input("Enter choice 1, 2 or 3:"))
if not (1 <= choice <= 3):
raise ValueError()
except ValueError:
print "Invalid Option, you needed to type a 1, 2 or 3...."
else:
print "Your choice is", choice
copied from: limit input to integer only (text crashes PYTHON program)

while True:
x=input("4,6 or 12? 0 to not roll: ")
if x.isalpha():
print ("only numbers.")
continue
elif int(x)==0:
print ("quiting..")
break
elif int(x)!=4 and int(x)!=6 and int(x)!=12:
print ("Not allowed.")
else:
print (random.randint(1,int(x)))
Here is another method, use isalpha().

Related

How to convert Celsius to Fahrenheit and vice-versa in python

I'm trying to create a program that will convert Celsius to Fahrenheit and vice-versa.
The first thing the program will do is ask the user what the user wants to convert either Celsius or Fahrenheit. If the input is invalid, print invalid and ask to try again.
Then will ask the user to input the start, end, and interval separated by an asterisk. For example 0102. This means that the start of the conversion will be from 0 to 10 with an interval of 2, thus the value to be converted will be 0, 3, 6, 9
If the start<end, then the interval should be 0<interval otherwise, your program will display an error and ask to try again
Or
If the start>end, then the interval should be 0>interval otherwise, your program will display an error and ask to try again.
If the user inputs has only one value, like 10. This means that the start is equal to 1 which will be the default value for start, up to 10, and the interval is 2 which will be the default value of the interval if start<end.
If the user input has two values like 10*2, this means that the start is equal to 10 up to 2. The interval is set to the default value which is equal to -2 since start>end.
This is my code, it doesn't work and I'm stuck.
And how am I gonna use for loop here?
while True:
pick = input("Enter your input either in Celsius or Fahrenheit: ")
if pick == "Celsius":
pass
elif pick == "Fahrenheit":
pass
else:
print("Invalid input. Try again.")
continue
while True:
sei = input("Enter the start, range, and interval separated by an asterisk(*): ").split("*")
if len(sei) == 3:
if int(sei[0].isdigit()) < int(sei[1].isdigit()) and 0 < int(sei[2].isdigit()):
pass
elif int(sei[0].isdigit()) > int(sei[1].isdigit()) and 0 > int(sei[2].isdigit()):
pass
else:
print("Error. Try again.")
continue
else:
print("Error. Try again")
Input :0 * 100 * 3
Output:
!
Then the program will ask the user to try again. If yes, the program will run from the very start.
If no, it'll print "Thank you" and the number of invalid inputs in the whole program and the number of times the user successfully converts temperature.
Here is my solution (inefficient maybe, I'm a beginner too) to your problem.
temperatureValues = []
while True:
pick = input("Enter your input either in Celsius or Fahrenheit: ")
if pick == "Celsius":
pass
elif pick == "Fahrenheit":
pass
else:
print("Invalid input. Try again.")
continue
while True:
sei = input("Enter the start, range, and interval separated by an asterisk(*): ").split("*")
if len(sei) == 3:
if int(sei[0]) < int(sei[1]) and 0 < int(sei[2]): # normal case
for i in range(int(sei[0]), int(sei[1]), int(sei[2])):
temperatureValues.append(i)
break
elif int(sei[0]) > int(sei[1]) and 0 > int(sei[2]): # reverse case
for i in range(int(sei[1]), int(sei[0]), int(sei[2])):
temperatureValues.append(i)
break
else:
print("Error. Try again.")
continue
elif len(sei) == 2:
for i in range(int(sei[0]), int(sei[1]), 2):
temperatureValues.append(i)
break
elif len(sei) == 1:
for i in range(1, int(sei[0]), 2):
temperatureValues.append(i)
break
else:
print("Error. Try Again.")
continue
print(temperatureValues)
# Implement your conversion here, by performing the conversion operation on each value of the temperatureValues list.
I would also advise you to do comparison in values by writing the variable first. Like int(sei[0]) > 0, instead of writing this in reverse. Makes the code more readable.
Best of luck!

Python IF ELSE statement not working [duplicate]

This question already has an answer here:
Simple Python IF statement does not seem to be working
(1 answer)
Closed 6 years ago.
I have my first program I am trying to make using python. The IF ELSE statement is not working. The output remains "Incorrect" even if the correct number is inputted by the user. I'm curious if it's that the random number and the user input are different data types. In saying that I have tried converting both to int with no avail.
Code below:
#START
from random import randrange
#Display Welcome
print("--------------------")
print("Number guessing game")
print("--------------------")
#Initilize variables
randNum = 0
userNum = 0
#Computer select a random number
randNum = randrange(10)
#Ask user to enter a number
print("The computer has chosen a number between 0 and 9, you have to guess the number!")
print("Please type in a number between 0 and 9, then press enter")
userNum = input('Number: ')
#Check if the user entered the correct number
if userNum == randNum:
print("You have selected the correct number")
else:
print("Incorrect")
On Python 3 input returns a string, you have to convert to an int:
userNum = int(input('Number: '))
Note that this will raise a ValueError if the input is not a number.
If you are using Python 3, change the following line:
userNum = input('Number: ')
to
userNum = int(input('Number: '))
For an explanation, refer to PEP 3111 which describes what changed in Python 3 and why.

Simple dice roll program issue

I have created a program where the user can select a certain sided dice then it rolls and outputs the number generated, it then asks if the user wants to roll again and by using a while loop. i have wrote the program and for some reason it keeps on repeating the input dice side number prompt and i don't know why, here is the code
import random
roll_agn='yes'
while roll_agn=='yes':
dice=input ('Please choose a 4, 6 or 12 sided dice: ')
if dice ==4:
print(random.randint(1,4))
elif dice ==6:
print(random.randint(1,6))
elif dice ==12:
print(random.randint(1,12))
else:
roll_agn=input('that is not 4, 6 or 12, would you like to choose again, please answer yes or no')
if roll_agn !='yes':
print ('ok thanks for playing')
I suspect it is something to do with the while loop or the indentation, but I have been fiddling with it for like 3o mins and i cant get it to work properly, so if someone could help me out here it would be appreciated, Thanks !
The indentation on else: roll_agn=input is such that it only runs after you exit the while loop - but the while loop can never end until you run the else clause, therefore infinite loop.
Here is a cleaned-up, better-structured version:
# assumes Python 3.x
from random import randint
def get_int(prompt):
while True:
try:
return int(input(prompt)) # if Python 2.x use raw_input instead of input
except ValueError:
# not an int
pass
def get_yn(prompt):
while True:
value = input(prompt).strip().lower() # if Python 2.x use raw_input instead of input
if value in {'y', 'yes'}:
return True
elif value in {'n', 'no'}:
return False
def roll(sides):
return randint(1, sides)
def main():
while True:
sides = get_int("Number of sides on die (4, 6, or 12)? ")
if sides in {4, 6, 12}:
print("You rolled a {}".format(roll(sides)))
else:
print("U no reed gud?")
if not get_yn("Play again (y/n)? "):
print("Thanks for playing!")
break
if __name__=="__main__":
main()
It looks like you have indentation problems with the if statements. Try lining up elif with if.
if dice ==4:
print(random.randint(1,4))
elif dice ==6:
print(random.randint(1,6))
elif dice ==12:
print(random.randint(1,12))

Number Guessing Game in Python

I was trying to create a simple random number guessing game. The problem is even if I type the correct number it replies with a 'The number is less than'. Can somebody provide me a solution for this one ?
Thanks in advance
import random
import sys
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9]
user = raw_input('Guess The Number\n Pick between 1 - 10\n >>> ')
try:
int(user)
except:
print "Numbers Only !"
sys.exit(0)
number = random.choice(numbers)
int(number)
for i in range(0, 4):
if number == user:
print 'You Won!'
if user > number:
print 'The number is less than', user
user = raw_input('>>> ')
try:
int(user)
except:
print "Numbers Only !"
if user < number:
print 'The number is bigger than', user
user = raw_input('>>> ')
int(user)
print "The Number was", number
The biggest problem is that you're not saving the conversion to int so you're using the guess as the string the user entered. You need to save it by doing user = int(raw_input('>>>'))
There are other ways you can improve this code, however. You repeat yourself a bit, and you don't need random.choice, you can use random.randrange(1, 10)
You shouldn't just say except:. You wanna only catch the exceptions you are looking for. The particular exception you are looking for is a ValueError
Additionally, I suggest you let the user try again when they enter something that's not a number. You can wrap up the whole thing in it's own function.
import random
def get_user_num(msg='>>> '):
"""Print the msg parameter as a prompt for the user to enter a number. If
they enter an invalid string, reprompt them until they enter a number.
"""
while True:
try:
return int(raw_input(msg)) # save the conversion to int
except ValueError: # only except the error you're actually looking for
print 'Numbers Only!'
# 'from 1-9' is probably better than 'between 1-10'
user = get_user_num('Guess The Number\n Pick from 1-9\n>>> ')
number = random.randrange(1, 10) # <- numbers list is unnecessary
#int(number) # this conversion was never needed, it was already a number
for _ in range(4): # you don't need (0, 4), 0 is assumed
if number == user:
print 'You Won!' # the correct number has been guessed
break # exit the loop once the number has been correctly guessed
elif user > number:
print 'The number is less than', user
elif user < number:
print 'The number is bigger than', user
# Don't repeat yourself, put this outside the `if`s
user = get_user_num()
else:
#only print the answer when it wasn't guessed correctly
print "The Number was", number
When you convert to int(user), you aren't saving a new int to user. So user still remains a string.
What you need to do is
user = int(user)
By the way, this is for all of the places where you use int(user)
This could be done with a much simpler implementation:
import random
number = random.randrange(10)
for i in xrange(4):
try:
user = int(raw_input('guess: '))
except ValueError:
print 'must be int'
continue
if user == number:
print 'bravo'
break
elif user < number:
print 'greater'
else:
print 'lesser'
print 'it was: %d' % number

Checking input is a number in python

I need help, my program is simulating the actions of a dice. I want to an error check to occur checking if the input string is a number and if it isn't I want to ask the question again again until he enters an integer
# This progam will simulate a dice with 4, 6 or 12 sides.
import random
def RollTheDice():
print("Roll The Dice")
print()
NumberOfSides = int(input("Please select a dice with 4, 6 or 12 sides: "))
Repeat = True
while Repeat == True:
if not NumberOfSides.isdigit() or NumberOfSides not in ValidNumbers:
print("You have entered an incorrect value")
NumberOfSides = int(input("Please select a dice with 4, 6 or 12 sides")
print()
UserScore = random.randint(1,NumberOfSides)
print("{0} sided dice thrown, score {1}".format (NumberOfSides,UserScore))
RollAgain = input("Do you want to roll the dice again? ")
if RollAgain == "No" or RollAgain == "no":
print("Have a nice day")
Repeat = False
else:
NumberOfSides = int(input("Please select a dice with 4, 6 or 12 sides: "))
As a commenter disliked my first answer with try: except ValueError and the OP asked about how to use isdigit, that's how you can do it:
valid_numbers = [4, 6, 12]
while repeat:
number_of_sides = 0
while number_of_sides not in valid_numbers:
number_of_sides_string = input("Please select a dice with 4, 6 or 12 sides: ")
if (not number_of_sides_string.strip().isdigit()
or int(number_of_sides_string) not in valid_numbers):
print ("please enter one of", valid_numbers)
else:
number_of_sides = int(number_of_sides_string)
# do things with number_of_sides
the interesting line is not number_of_sides_string.strip().isdigit(). Whitespace at both ends of the input string is removed by strip, as a convenience. Then, isdigit() checks if the full string consists of numbers.
In your case, you could simply check
if not number_of_sides_string not in ['4', '6', '12']:
print('wrong')
but the other solution is more general if you want to accept any number.
As an aside, the Python coding style guidelines recommend lowercase underscore-separated variable names.
Capture the string in a variable, say text. Then do if text.isdigit().
Make a function out of:
while NumberOfSides != 4 and NumberOfSides != 6 and NumberOfSides != 12:
print("You have selected the wrong sided dice")
NumberOfSides = int(input("Please select a dice with 4, 6 or 12 sides: "))
And call it when you want to get input. You should also give an option to quit e.g. by pressing 0. Also you should try catch for invalid number. There is an exact example in Python doc. Note that input always try to parse as a number and will rise an exception of it's own.
You can use type method.
my_number = 4
if type(my_number) == int:
# do something, my_number is int
else:
# my_number isn't a int. may be str or dict or something else, but not int
Or more «pythonic» isinstance method:
my_number = 'Four'
if isinstance(my_number, int):
# do something
raise Exception("Please, enter valid number: %d" % my_number)

Categories