简体   繁体   English

(!)比较变量时无休止的循环,我不知道为什么?

[英](!) Endless loop when comparing variables and I don't know why?

I have created this code to get 3 different options in 3 different places. 我创建了此代码,以便在3个不同的地方获得3个不同的选项。 Its actually a flash card program i hoped to get working but I can't. 它实际上是一个闪存卡程序,我希望可以开始使用,但我不能。 It goes into a endless loop and i have no idea why. 它陷入无尽的循环,我不知道为什么。 There may also be other problems but i havent got to them yet but please tell me anyway. 可能还有其他问题,但我还没有解决,但无论如何请告诉我。 Keep the sam var names so i can understand easily. 保留sam var名称,以便我轻松理解。 I have attached all the code. 我已经附上了所有代码。 They is some more but its not been implemented yet. 它们更多,但尚未实施。

There is also 3 lists each with 14 items but these won't go into code: 还有3个列表,每个列表包含14个项目,但这些代码不会包含在代码中:

key_words = ['Cellulose', 'Respiration', 'Haemoglobin', 'Ventilation', 'Cartilage', 'Cytoplasm', 'Nucleus', 'Alveoli', 'Amino acids', 'Virus', 'White blood cells', 'Photosynthesis', 'Stomata', 'Vaccine', 'Fibre'] key_words = [“纤维素”,“呼吸”,“血红蛋白”,“通气”,“软骨”,“细胞质”,“核仁”,“肺泡”,“氨基酸”,“病毒”,“白细胞”, [光合作用],[气孔],[疫苗],[纤维]]

defs = ['Tough substance that makes up the cell walls of green plants', 'A chemical reaction that causes energy to be released from glucose', 'A substance which joins to oxygen and carries it round the body in the blood', 'Breathing', 'Tough, smooth substance covering the ends of bones to protect them', 'Jelly-like part of a cell where chemical reactions happen', 'Controls what happens inside a cell', 'Tiny air sacs in the lungs', 'Produced when proteins are digested', 'The smallest type of microbe', 'Can engulf bacteria or make antibodies', 'The process of turning carbon dioxide, water and light into glucose and oxygen', 'Small holes in the underside of a leaf', 'Dead or inactive forms of a microorganism', 'A nutrient that cannot be digested'] defs = [“构成绿色植物细胞壁的坚硬物质”,“一种化学反应,它使能量从葡萄糖中释放出来”,“一种与氧结合并带入血液中的物质,”呼吸”,“坚硬,光滑的物质覆盖骨头的末端以保护骨头”,“发生化学反应的细胞的果冻状部分”,“控制细胞内部发生的事情”,“肺部微小的气囊”, '当蛋白质被消化时产生','最小的微生物','可以吞噬细菌或制造抗体','将二氧化碳,水和光转化为葡萄糖和氧气的过程','在容器底部的小孔叶子”,“微生物的死亡或失活形式”,“无法消化的营养物”]

completed = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] 已完成= [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]

thanks Callum 谢谢卡勒姆

import random
option1 = random.randint(int(1), int(14))
option2 = random.randint(int(1), int(14))
option3 = random.randint(int(1), int(14))

while option1 == option2 or option1 == option3:
    placement1 = random.randint(int(1), int(3))

while option2 == option3:
    option2 = random.randint(int(1), int(3))


placement1 = random.randint(int(1), int(3))
placement2 = random.randint(int(1), int(3))
placement3 = random.randint(int(1), int(3))

while placement1 == placement2 or placement1 == placement3:
    placement1 = random.randint(int(1), int(3))

while placement2 == placement1 or placement2 == placement3:
    placement3 = random.randint(int(1), int(3))

print('What is the correct defenition for', key_words[option3])
place3 = 1

if placement1 == 1:
    print('1: ', defs[option1])
elif placement1 == 2:
    print('1: ', defs[option2])
elif placement1 == 3:
    print('1: ', defs[option3])
    place3 = '1'
if placement2 == 1:
    print('2: ', defs[option1])
elif placement2 == 2:
    print('2: ', defs[option2])
elif placement2 == 3:
    print('2: ', defs[option3])
    place3 = '2'
if placement3 == 1:
    print('3: ', defs[option1])
elif placement3 == 2:
    print('3: ', defs[option2])
elif placement3 == 3:
    print('3: ', defs[option3])
    place3 = '3'


choice = str(input('Enter 1, 2 or 3: '))
if choice == place3:
    print('Well done, correct.')
    a = completed[option3] + 1
    completed[option3] += 1
else:
    print('Inccorect. Have another look and we`ll come back later.')

In your first loop: 在您的第一个循环中:

while option1 == option2 or option1 == option3:
    placement1 = random.randint(int(1), int(3))

you never change the value of option1 . 您永远不会更改option1的值。 If the condition is true going into the loop, it will remain true forever. 如果条件为真,则进入循环,它将永远保持为真。 Did you mean to use option1 instead of placement1 ? 您是要使用option1而不是placement1吗?

You will never break out of your first loop. 您将永远不会脱离第一个循环。

while option1 == option2 or option1 == option3:
    placement1 = random.randint(int(1), int(3))

The condition hinges on the values of option1, option2, and option3, which are never adjusted in the body of the loop. 条件取决于option1,option2和option3的值,这些值从未在循环主体中进行调整。 If the code enters the loop, it will stay there. 如果代码进入循环,它将停留在那里。

Incidentally, this code has numerous other serious problems and code smells. 顺便说一句,此代码还有许多其他严重的问题和代码气味。 I don't have time to name them all. 我没有时间将它们全部命名。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM