python 有人帮忙看看这个代码可以简化一下吗
import randomanswer=random.randint(1,15)
time=0
rest=5
print("-----I am destiny-----")
print("你只有5次机会")
temp = input("猜猜我在想0-15中的哪个数字:")
guess = int(temp)
if guess == answer:
print("。。。")
print("恭喜你!")
else:
if guess<answer:
print("嘿,小了,小了")
else:
print("大了,大了")
time+=1
rest=rest-1
print("还有",rest,"次机会")
while guess!=answer and time<5:
time+=1
rest=rest-1
temp = input("再猜猜:")
guess = int(temp)
if guess == answer:
print("。。。")
print("恭喜你!")
else:
if guess<answer:
print("嘿,小了,小了")
else:
print("大了,大了")
print("还有",rest,"次机会")
print("好了不玩了")
谢谢各位! 为何要写两遍?
import random
answer=random.randint(1,15)
time=5
print("-----I am destiny-----")
print("你只有5次机会")
while time:
temp = input("猜猜我在想0-15中的哪个数字:")
guess = int(temp)
if guess == answer:
print("。。。")
print("恭喜你!")
break
else:
if guess<answer:
time -= 1
print("嘿,小了,小了")
print("您还有%d次机会,再试一次吧" % time)
continue
else:
time -= 1
print("大了,大了")
print("您还有%d次机会,再试一次吧" % time)
continue
print("游戏结束")
直接套进while循环里就完事,
而且你定义一个time,又来个rest
就让人很迷 本帖最后由 heidern0612 于 2020-3-31 08:04 编辑
import random
answer=random.randint(1,15)
time=0
rest=0
print("-----I am destiny-----")
print("你只有5次机会")
whilerest < 5 :
temp = input("猜猜我在想0-15中的哪个数字:")
guess = int(temp)
if guess == answer:
print("。。。")
print("恭喜你!")
break
else:
if guess < answer:
print("嘿,小了,小了",end = " ")
else:
print("大了,大了",end = " ")
rest +=1
if rest == 5 :
print("猜的次数太多啦,不玩啦")
else:
print(f"还有{5-rest}次机会。")
写两遍干嘛,加个while不就行了 本帖最后由 helloTOM 于 2020-3-31 08:37 编辑
import random
answer=random.randint(1,15)
rest=5
print("-----I am destiny-----")
print("你只有5次机会")
guess = int(input("猜猜我在想0-15中的哪个数字:"))
while True:
if guess == answer:
print("。。。\n恭喜你!")
break
elif guess<answer:
print("嘿,小了,小了")
elif guess>answer:
print("大了,大了")
rest=rest-1
print("还有",rest,"次机会")
if rest == 0:
break
guess = int(input("再猜猜:"))
print("好了不玩了") 本帖最后由 永恒的蓝色梦想 于 2020-3-31 08:38 编辑
answer=random.randint(1,15)
temp = input("猜猜我在想0-15中的哪个数字:")有点东西,明明是1-15非要说成0-15
from random import randint
ans=randint(0,15)
print("-----I am destiny-----\n你只有5次机会")
times=5
while times:
temp=int(input("猜猜我在想0-15中的哪个数字:"))
if temp==ans:
print("。。。\n恭喜你!")
break
elif temp>ans:
print("大了,大了")
elif temp<ans:
print("嘿,小了,小了")
times-=1
print("还有%d次机会"%times)
print('好了不玩了')这个就很简单了吧{:10_327:} qiuyouzhi 发表于 2020-3-31 07:55
为何要写两遍?
直接套进while循环里就完事,
循环最后加个continue做什么? 直接循环,不用写两遍。 学习了~~~ import random ; answer = random.randint(1,15) ; time=0 ; rest=5
print("-----I am destiny-----\n你只有5次机会")
temp = input("猜猜我在想1-15中的哪个数字:") ; guess = int(temp)
while guess!=answer and time<5:
time+=1 ; rest -= 1
if guess == answer:
print("。。。\n恭喜你")
else:
if guess<answer:
print("嘿,小了,小了")
else:
print("大了,大了")
print("还有",rest,"次机会")
temp = input("再猜猜:") ; guess = int(temp)
print("好了不玩了")
这样偷工减料 哈哈哈哈哈{:9_219:}你们应该看不见 本帖最后由 txxcat 于 2020-3-31 11:47 编辑
while循环换成for循环,精简次数累进,用\n精简print语句,用三元表达式精简if语句:
import random
answer=random.randint(0,15)
print("-----I am destiny-----\n你只有5次机会\n猜猜我在想0-15中的哪个数字:",end='')
for i in range(5):
guess = int(input())
if guess == answer:
print("。。。\n恭喜你!")
break
elif i<4:
print("嘿,小了,小了" if guess< answer else "大了,大了",("\n还有%s次机会" % (4-i)),"\n再猜猜:",end='')
print("好了不玩了!") 参考前面所有大佬的答案写个作业{:9_231:}
import random
answer=random.randint(1,15)
time=5
print("-----I am destiny-----")
print("你只有5次机会")
while time :
temp = input("猜猜我在想1-15中的哪个数字:")
guess = int(temp)
if guess == answer:
print("。。。\n恭喜你!")
break
else:
if guess<answer:
time = time-1
print("嘿,小了,小了\n再猜猜:" )
else:
time = time-1
print("大了,大了\n再猜猜:" )
print("还有",time,"次机会")
continue
print("好了不玩了") sakanakun 发表于 2020-3-31 15:36
参考前面所有大佬的答案写个作业
15和19行可以合并,因为无论是if还是else,都要执行这句,把这句移到循环外就可以了:
if guess<answer:
print("嘿,小了,小了\n再猜猜:" )
else:
print("大了,大了\n再猜猜:" )
time = time-1
print("还有",time,"次机会")
然后,continue的意思说不再继续执行循环体后面的语句,直接回到开头,这里已经是循环最后了,无论如何都会回到开头,所以没必要。 txxcat 发表于 2020-3-31 17:23
15和19行可以合并,因为无论是if还是else,都要执行这句,把这句移到循环外就可以了:
然后,continu ...
原来如此,谢谢大佬指点{:5_108:}
页:
[1]