I've been working on this for the last few days. Any improvement ideas? (Notes are very simple, making this for school and notes have to be for complete novices to Python).
import random # Allows the computer to generate random numbers, vital for the program to function correctly.
import sys # Allows a new selection of system related commands to be used; I use sys.exit() to close the program.
def oneToTen():
ten = random.randint(1,10) #Generates a random integer between 1 and 10.
attempts = 1
guess = int(input("Make your guess!"))
while guess != ten: #Everything indented below this will happen when the user's guess is NOT equal to the random number. != means not equal to.
if guess < ten: #This will happen when the user's guess is lower than the randomly generated number.
print("Higher!")
attempts = attempts + 1 #Adds one to the number of attempts the user has taken to guess the number.
guess = int(input("Make your guess!"))
elif guess > ten: #This will happen when the user's guess is higher than the randomly generated number.
print("Lower!")
attempts = attempts + 1
guess = int(input("Make your guess!"))
if guess > 10: #This will happen when the user's guess is larger than 10.
print("Enter a valid number!")
guess = int(input("Make your guess!"))
elif guess == ten: #This will happen when the user's guess is equal to the random number.
print("Well done! You guessed it in",attempts,"tries!")
end = input()
sys.exit() #Exits the program.
elif attempts == 5:
print("The number was",ten,"!")
end = input("Maximum amount of tries reached!")
sys.exit()
def oneToTwenty():
twenty = random.randint(1,20)
attempts = 1
guess = int(input("Make your guess!"))
while guess != twenty:
if guess < twenty:
print("Higher!")
attempts = attempts + 1
guess = int(input("Make your guess!"))
elif guess > twenty:
print("Lower!")
attempts = attempts + 1
guess = int(input("Make your guess!"))
if guess > 20:
print("Enter a valid number!")
guess = int(input("Make your guess!"))
elif guess == twenty:
print("Well done! You guessed it in",attempts,"tries!")
end = input()
sys.exit()
elif attempts == 5:
print("The number was",twenty,"!")
print("Maximum amount of tries reached!")
sys.exit()
def oneToFifty():
fifty = random.randint(1,50)
attempts = 1
guess = int(input("Make your guess!"))
while guess != fifty:
if guess < fifty:
print("Higher!")
attempts = attempts + 1
guess = int(input("Make your guess!"))
elif guess > fifty:
print("Lower!")
attempts = attempts + 1
guess = int(input("Make your guess!"))
if guess > 50:
print("Enter a valid number!")
guess = int(input("Make your guess!"))
elif guess == fifty:
print("Well done! You guessed it in",attempts,"tries!")
end = input()
sys.exit()
elif attempts == 5:
print("The number was",fifty,"!")
print("Maximum amount of tries reached!")
sys.exit()
def oneToHundred():
hundred = random.randint(1,100)
attempts = 1
guess = int(input("Make your guess!"))
while guess != hundred:
if guess < hundred:
print("Higher!")
attempts = attempts + 1
guess = int(input("Make your guess!"))
elif guess > hundred:
print("Lower!")
attempts = attempts + 1
guess = int(input("Make your guess!"))
if guess > 100:
print("Enter a valid number!")
guess = int(input("Make your guess!"))
elif guess == hundred:
print("Well done! You guessed it in",attempts,"tries!")
end = input()
sys.exit()
elif attempts == 5:
print("The number was",hundred,"!")
print("Maximum amount of tries reached!")
sys.exit()
print("Hello! In this game, you shall attempt to guess my number!") #What the user sees upon opening the program.
print("What range would you like my number to be in?")
print("1. 1-10")
print("2. 1-20")
print("3. 1-50")
choice = int(input("4. 1-100")) #User's choice of the range of numbers.
if choice == 1: #Executes the code for a number between 1 and 10.
oneToTen()
if choice == 2: #Executes the code for a number between 1 and 20.
oneToTwenty()
if choice == 3: #Executes the code for a number between 1 and 50.
oneToFifty()
if choice == 4: #Executes the code for a number between 1 and 100.
oneToHundred()