It takes 2 minutes to read this article
Python basic actual combat-guess the age game
age =18 #answer
count =0 #Game control
prize_dict ={0:'Ragdoll',1:'Transformers',2:'Ultraman',3:'<Python from entry to giving up>'}
# Core code
while count <3:
inp_age =input('Please enter your age>>>') #Interact with the user
# Determine whether the user is harassing(Super class: Determine whether the user input is a number)if not inp_age.isdigit():print('Stupid,Your age is wrong')continue
inp_age_int =int(inp_age)
# Core logic,Judging age
if inp_age_int == age:print('Got it right')print(prize_dict) #Print prizes
# Get two prizes
for i inrange(2):
prize_choice =input('Please enter the prize you want,If you don't want,Then enter"n"drop out!!!') #Interact with users to get prizes
# Determine whether a prize is needed
if prize_choice !='n':print(f'Congratulations on your prize: {prize_dict[int(prize_choice)]}')else:breakbreak
elif inp_age_int < age:print('Guessed')else:print('Guess big')
count +=1 #Play a game successfully
if count !=3:continue
again_choice =input('Whether to continue the game,Please enter"Y",Otherwise, any key will exit directly.') #Whether to interact again
# Judge whether to continue
if again_choice =='Y':
count =0
operation result:
Please enter your age>>>18
Got it right
{0:' Ragdoll',1:'Transformers',2:'Ultraman',3:'<Python from entry to giving up>'}
Please enter the prize you want,If you don't want,Then enter"n"drop out!!!0
Congratulations on your prize:Ragdoll
Please enter the prize you want,If you don't want,Then enter"n"drop out!!!1
Congratulations on your prize:Transformers
Recommended Posts