python竞猜商品价格
时间: 2023-11-06 10:03:10 浏览: 179
商品价格竞猜
Python竞猜商品价格的程序可以通过以下代码实现:
import random
def guess_price():
secret_price = random.randint(1, 100)
guesses_left = 5
print("欢迎参加商品价格竞猜游戏!")
while guesses_left > 0:
guess = int(input("请输入你猜测的价格:"))
if guess < secret_price:
print("价格低了,请继续猜测!")
elif guess > secret_price:
print("价格高了,请继续猜测!")
else:
print("恭喜你,猜对了!")
break
guesses_left -= 1
if guesses_left == 0:
print("很遗憾,你没有猜对。正确的价格是", secret_price)
guess_price()
阅读全文