Python exercise 9 - Guessing Game

本文介绍了一个使用Python编写的简单猜数字游戏。游戏会生成一个1到9之间的随机数,玩家需要猜测这个数字,系统将提示玩家猜的数字是否过高、过低或正确。游戏还包含了继续猜测或退出游戏的选项,并记录了玩家的猜测次数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Exercise 9 - Guessing Game

'''
Generate a random number between 1 and 9 (including 1 and 9). Ask the user to guess the number, then tell them whether they guessed too low, too high, or exactly right. (Hint: remember to use the user input lessons from the very first exercise)

Extras:

Keep the game going until the user types “exit”
Keep track of how many guesses the user has taken, and when the game ends, print this out.
'''

Code:

import random
print ('Guessing Game starts')
random_num = random.randint(1,9) #give a random number
guess_count =0
game =''
while game!='exit':
	guess_num = int(input('Please enter the number you guess between 1 to 9: '))
	guess_count+=1
	if random_num==guess_num:
		print ('Congratulations! the number you guess is exactly right')
		game='exit'
	elif random_num<guess_num:
		print ('the number you guess is too high. Please choose continue guess or exit.')
		game=input('yes to continue, exit to stop th game: ')
	else:
		print ('the number you guess is too low. Please choose continue guess or exit.')
		game=input('yes to continue, exit to stop th game: 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值