0

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')
1
  • 2
    Why use a set at all? 1 <= x <= 9 or x in range(1, 10) would do. Commented Apr 28, 2022 at 4:28

3 Answers 3

3

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')
Sign up to request clarification or add additional context in comments.

Comments

0

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.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.