Home > Enterprise >  Using If Else statements in addition to sets using Python
Using If Else statements in addition to sets using Python

Time:04-28

I have to create a fortune teller for school. I want the user to input a number between 1-9. But i also want to give an error message if they put in a different number. I'm using a set containing my numbers, but I can't call it in my if statements.

fortune_num = set([1, 2, 3, 4, 5, 6, 7, 8, 9])
user_num = input(f'Pick a number and find your fortune! Choose a number from 1 to 9 and hit enter: ')
print()
if user_num == fortune_num:
    print(user_num)
else:
    print('Error')

CodePudding user response:

Use the keyword in to check set membership, also cast input into int:

fortune_num = set([1, 2, 3, 4, 5, 6, 7, 8, 9])
user_num = input(f'Pick a number and find your fortune! 
\nChoose a number from 1 to 9 and hit enter: ')
print()
if int(user_num) in fortune_num: #this would throw an error if input is not int
    print(user_num)
else:
    print('Error')

CodePudding user response:

  1. if user_num == fortune_num: // instead of this; you can use keyword in int(user_num) in fortune_num:,

  2. Also your print statement must be indented, otherwise you might get indentation error.

CodePudding user response:

You can use this code,

fortune_num = list(range(1,10))
user_num = input(f'Pick a number and find your fortune!\nChoose a number from 1 to 9 and hit enter: ')

if int(user_num) in fortune_num:
  print(user_num)
else:
  raise ValueError("You entered a wrong input!")

This would raise an actual Python error if the input is wrong.

If you want to give the user another chances to enter a correct input use this,

fortune_num = list(range(1,10))

while True:
  try:
    user_num = int(input(f'Pick a number and find your fortune!\nChoose a number from 1 to 9 and hit enter: '))
    if user_num in fortune_num:
      print(user_num)
      break
    else:
      raise ValueError

  except:
    print("\nYou entered a wrong input. Please enter a valid number!")

Change the code as per your needs but this would do work as the perfect foundation.

CodePudding user response:

First of all, input takes user input as a string. So even if they enter 1, that won't match your set, because your set is of int, and their input is the string '1', not the integer 1. Also, there's no need to use a set. A range object is easier to generate, and since it contains multiple numbers, the variable name should be plural. You also don't have your indentation correct. I don't see what the f in the input function is doing, and if you want a multi-line string, you need triple quotes. Also, if you have a carriage return in your string, putting \n in the string gives you two line breaks; I'm not sure that's intended. So one way of doing this is:

fortune_nums = [str(num) for num in range(1,10)]
user_num = input('''Pick a number and find your fortune!
Choose a number from 1 to 9 and hit enter: \n''')
print()
if user_num in fortune_nums:
    print(user_num)
else:
    print('Error')

If you want to get fancier, you can keep your fortune_nums as int, then do a try-except on converting the input to int, catching the invalid literal error:

fortune_nums = range(1,10)
user_num = input('''Pick a number and find your fortune!
Choose a number from 1 to 9 and hit enter: \n''')
print()
try:
    if(int(user_num) in fortune_nums):
        print(user_num)
except ValueError:
    print("That's not a valid integer!")
  • Related