Lottery Game
unknown
python
6 months ago
4.9 kB
2
Indexable
""" Describe your program here. You must describe your program for a full credit. 3. Write a Python program that asks the user to enter a number between 100 and 500 inclusive. Make sure the number entered by the user is in the given range. If the user-entered number is not in the range, display an appropriate message to the user and repeatedly ask the user to enter a number in the range. If the number is in the range but not equal to a computer-generated random number, assist the user by telling them to guess higher or lower three times including the first attempt. If the user did not win on the third attempt, display a message based on the following criteria. → random number == user number; they win $10,000 in lottery. → random number - user number < 100; they win $7,000 in lottery. → random number - user number < 200; they win $5,000 in lottery. → random number - user number < 300; they win $3,000 in lottery. → random number - user number < 400; they win $1,000 in lottery. → random number - user number >= 400; You didn't win; please try again. Allow the user to play again as much as they like. Input: Enter a number between 100 and 500 to win lottery: a Output: Your input is invalid. Enter a number between 100 and 500 to win lottery: 100 Input: Enter a number between 100 and 500 to win lottery: 50 Output: You entered a number out of range. Enter a number between 100 and 500 to win lottery: 100 Output: You won {amount} in lottery. Do you wish to play the game again? (y or n): or Sorry, You did not win; please try again. Do you wish to play the game again? (y or n): Use must use variables such as choice, user_number, amount, random_number, etc. Hint: You will need to validate the user_number. """ choice = "y" # Write your code below this line # =============================== import random import math # Main loop for the lottery game while True: # Generate a random number between 100 and 500 random_number = random.randint(100, 500) while True: # Ask the user for input user_input = input("Enter a number between 100 and 500 to win lottery: ") # Check if the input is a valid integer if user_input.isdigit(): user_number = int(user_input) # Validate the user input if user_number < 100 or user_number > 500: print("You entered a number out of range.") continue # Ask again else: print("Your input is invalid.") continue # Ask again # User input is valid, check the guess for attempt in range(1, 4): if user_number == random_number: print(f"You guessed the magic number! {random_number}!") print("You won $10,000 in lottery!") break # User wins, exit the guessing loop elif user_number > random_number: print("Too high!") else: print("Too low!") # Only allow 3 attempts if attempt < 3: user_input = input(f"Enter a number between 100 and 500: ") attempt +=1 if user_input.isdigit(): user_number = int(user_input) else: print("Your input is invalid.") break # Exit the guessing loop if input is invalid else: # User failed to guess correctly after 3 attempts if random_number - user_number < 100: amount = 7000 print(f"Your guess {user_number} The lottery number was {random_number}") print(f"You won ${amount} in lottery!") elif random_number - user_number < 200: amount = 5000 print(f"Your guess {user_number} The lottery number was {random_number}") print(f"You won ${amount} in lottery!") elif random_number - user_number < 300: amount = 3000 print(f"Your guess {user_number} The lottery number was {random_number}") print(f"You won ${amount} in lottery!") elif random_number - user_number < 400: amount = 1000 print(f"Your guess {user_number} The lottery number was {random_number}") print(f"You won ${amount} in lottery!") else: print("Sorry, you did not win; please try again.") # Ask if the user wants to play again choice = input("Do you wish to play the game again? (y or n): ").lower() if choice == 'n': print("Thank you for playing!") break # breaks the current for loops break #Exit the main game loop
Editor is loading...
Leave a Comment