gpt4 book ai didi

python - continue 无法在 for 循环中使用 if 语句 ('continue' 无法正确地在循环中工作)

转载 作者:行者123 更新时间:2023-12-01 08:24:49 28 4
gpt4 key购买 nike

编码新手,无法弄清楚为什么 continue 在我的 for 循环中不起作用。在打印语句之后,我想从顶部重新开始循环。如果有人能让我知道我做错了什么,那就太好了。我不允许共享 cse231_random 文件,抱歉,如果这导致您无法运行它。

import string
import random
import time
from cse231_random import randint
ALPHABET_EASY = string.ascii_letters
ALPHABET = string.ascii_letters + string.punctuation


easy_str = ""
length=random.randint(3, 5)
for i in range(length):
index=randint(0, len(ALPHABET_EASY))
easy_str += ALPHABET_EASY[index]
print(easy_str)
start= time.time()
easy_input=input("\nEnter this string:")
stop= time.time()
easy_input=easy_input.replace(" ", "")
easy_input=easy_input
if stop-start>10:
print("Oops! Too much time.")
continue
elif easy_input.lower()==easy_str.lower():
print("Good job! You spent {} of 10 seconds entering string [{}][{}]".format(stop-start,easy_str,easy_input))
continue
elif easy_input.lower()!=easy_str.lower():
print("Incorrect.")
continue

最佳答案

整个正文应该缩进吗?

for i in range(length):
index=randint(0, len(ALPHABET_EASY))
easy_str += ALPHABET_EASY[index]
print(easy_str)
start= time.time()
easy_input=input("\nEnter this string:")
stop= time.time()
easy_input=easy_input.replace(" ", "")
easy_input=easy_input
if stop-start>10:
print("Oops! Too much time.")
continue
elif easy_input.lower()==easy_str.lower():
print("Good job! You spent {} of 10 seconds entering string [{}][{}]".format(stop- start,easy_str,easy_input))
continue
elif easy_input.lower()!=easy_str.lower():
print("Incorrect.")
continue

关于python - continue 无法在 for 循环中使用 if 语句 ('continue' 无法正确地在循环中工作),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54338007/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com