设计了一个猜数字的程序,别人用了十几行,我这六十行了,有谁知道如何让这个代码更简洁,求大神指点!!!

发布时间:2023年12月18日

程序要求:猜一个随机的一到十的数字,三次机会,每次猜不中会提示大了或小了

以下是我编写的代码:
import random
num = random.randint(1,10)
guess = int(input("请输入你猜测的数字(1~10):"))
if guess==num:
??? print("你猜对了")
elif guess> num:
??? print("你猜大了")
??? guess = int(input("请输入你猜测的数字(1~10):"))
??? if guess == num:
??????? print("你猜对了")
??? elif guess > num:
??????? print("你猜大了")
??????? guess = int(input("请输入你猜测的数字(1~10):"))
??????? if guess == num:
??????????? print("你猜对了")
??????? elif guess > num:
??????????? print("你猜大了")
??????????? print(num)
??????? elif guess < num:
??????????? print("你猜小了")
??????????? print(num)
??? elif guess < num:
??????? print("你猜小了")
??????? guess = int(input("请输入你猜测的数字(1~10):"))
??????? if guess == num:
??????????? print("你猜对了")
??????? elif guess > num:
??????????? print("你猜大了")
??????????? print(num)
??????? elif guess < num:
??????????? print("你猜小了")
??????????? print(num)
elif guess< num:
??? print("你猜小了")
??? guess = int(input("请输入你猜测的数字(1~10):"))
??? if guess == num:
??????? print("你猜对了")
??? elif guess > num:
??????? print("你猜大了")
??????? guess = int(input("请输入你猜测的数字(1~10):"))
??????? if guess == num:
??????????? print("你猜对了")
??????? elif guess > num:
??????????? print("你猜大了")
??????????? print(num)
??????? elif guess < num:
??????????? print("你猜小了")
??????????? print(num)
??? elif guess < num:
??????? print("你猜小了")
??????? guess = int(input("请输入你猜测的数字(1~10):"))
??????? if guess == num:
??????????? print("你猜对了")
??????? elif guess > num:
??????????? print("你猜大了")
??????????? print(num)
??????? elif guess < num:
??????????? print("你猜小了")
??????????? print(num)

想变简洁一点,求指导!!!

文章来源:https://blog.csdn.net/Yi_Lun_Ming_Yue/article/details/132757814
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。