鱼C论坛

 找回密码
 立即注册
查看: 3007|回复: 8

[已解决](急救)如何让while循环5次后break?????

[复制链接]
发表于 2017-9-3 11:02:57 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
这是我的代码
import random

score = 0
count = 0
list_index = [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19]
list_1 = ["NewZealand","love","walk","meeting","food","hello","activity","shark","stomach","canoe","family","reputation","mirror","boy","girl","sun","wind","water","thunder","fire"]  
list_2 = ["Aotearoa","aroha","hikoi","hui","kai","Kiaora","mahi","mako","puku","waka","whanau","mana","karahe","tamaititane","kotiro","komaru","touarangi","katao","wahitiri","kanaku"]
while True:
   
    index_1 = random.choice(list_index)
    index_2 = random.choice(list_index)
    if index_1 == index_2:
        print("How to say the English word in Maori language below ?")
        print(list_1[index_1])
        enter = str(input("Please enter the answer:"))
        if enter in list_2:
            print("Correct, you get one mark!")
            score = score + 1
        else:print("Sorry, you are wrong.Answer is",list_2[index_2])

print("Game over, your total score is %d."%(score))

如何让while里面的代码循环5次后break,我之前试过用if count ==5:break
可能是地方放错了。我实在是不知道怎么搞了,已经上网读文章和自己摸索了3个小时,求论坛内的朋友帮忙指点迷津,多谢多谢
最佳答案
2017-9-4 08:21:56
while i 是当i不为0时执行,为0结束。
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2017-9-3 11:24:12 | 显示全部楼层
  1. count = 0
  2. while count < 5:

  3.     count += 1
复制代码

  1. count = 0
  2. while True:
  3.     if count == 5:
  4.         break

  5.     count += 1
复制代码

  1. for i in range(5):
  2.     print(i)
复制代码

小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-9-3 11:27:35 | 显示全部楼层
2017-09-03_112710.png
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2017-9-3 13:25:53 | 显示全部楼层
感谢各位回答,不过我逐个试验过了,还是不行啊,请大家在帮帮忙,多谢多谢
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-9-3 14:07:59 | 显示全部楼层
你可以在循环前定义一个变量(比如a)为5, 然后在这个循环的结尾设置
  1. a -= 1
复制代码

然后写一个判断句,判断a等于0时break
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-9-3 14:11:40 | 显示全部楼层
或者你不用写判断句,写
  1. while a:
复制代码

也可以
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-9-3 14:18:16 | 显示全部楼层
  1. import random
  2. score = 0
  3. count = 0
  4. list_index = [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19]
  5. list_1 = ["NewZealand","love","walk","meeting","food","hello","activity","shark","stomach","canoe","family","reputation","mirror","boy","girl","sun","wind","water","thunder","fire"]  
  6. list_2 = ["Aotearoa","aroha","hikoi","hui","kai","Kiaora","mahi","mako","puku","waka","whanau","mana","karahe","tamaititane","kotiro","komaru","touarangi","katao","wahitiri","kanaku"]
  7. i=5
  8. while i:
  9.     i=i-1
  10.     index_1 = random.choice(list_index)
  11.     index_2 = random.choice(list_index)
  12.     if index_1 == index_2:
  13.         print("How to say the English word in Maori language below ?")
  14.         print(list_1[index_1])
  15.         enter = str(input("Please enter the answer:"))
  16.         if enter in list_2:
  17.             print("Correct, you get one mark!")
  18.             score = score + 1
  19.         else:print("Sorry, you are wrong.Answer is",list_2[index_2])

  20. print("Game over, your total score is %d."%(score))
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2017-9-4 05:43:38 | 显示全部楼层

亲啊,感谢你的回答,不过你发的好像有错啊,while i 是啥啊?求解释,多谢多谢
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-9-4 08:21:56 | 显示全部楼层    本楼为最佳答案   
while i 是当i不为0时执行,为0结束。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2026-3-2 02:50

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表