Python - 如何在回答后保持循环输入?
Python - How to keep the loop input after it answered?
Main.py
在我现在的代码中,它只是停留在第一个问题上而没有继续第二个问题。我想让它循环直到回答最后一个问题并且循环将 break/stop.
def main():
Total = None
with open("test.json", "r") as f:
File = json.load(f)
for i in File["data"].keys():
Total = i # Total of the keys
def do_game():
num = 0
num += 1
if num > int(Total):
print("No more questions are available.")
return False # To break the loop but didn't work at all
name = File["data"][str(num)]["type"]
print("Enter type of filename (#{}) :".format(num))
answer = input("Answer: ")
if answer == name:
print("Correct!")
else:
print("Incorrect!")
while True:
do_game() # I tried to loop this but didn't go well
main()
test.json
{
"questions": {
"1": {
"question": "Video.mp4",
"answer": "mp4"
},
"2": {
"question": "cool.gif",
"answer": "gif"
},
"3": {
"question": "main.py",
"answer": "python"
}
}
}
尝试将循环调整为:
while True:
result = do_game()
if result == False:
break
你可以试试这个:
def main():
Total = None
with open("test.json", "r") as f:
File = json.load(f)
for i in File["data"].keys():
Total = i # Total of the keys
num = 1
while num <= int(Total):
name = File["data"][str(num)]["type"]
print("Enter type of filename (#{}) :".format(num))
answer = input("Answer: ")
if answer == name:
print("Correct!")
else:
print("Incorrect!")
num += 1
main()
因为你的数据是从1开始计数所以我们设置num = 1
.
您的代码根本就不是 运行 question
1 和 2(这是您数据的前 2 项)。它不会对文件进行任何循环。
您需要修改代码。试试这个,
import json
def main():
with open("test.json", "r") as f:
File = json.load(f)
total = len(File['questions'])
for i in range(1,total+1):
name = File["questions"][str(i)]["answer"]
print("Enter type of filename (#{}) :".format(i))
answer = input("Answer: ")
print("Correct!" if answer==name else "Incorrect!")
else:
print("No more questions are available.")
return False # To break the loop but didn't work at all
main()
输出:
Enter type of filename (#1) :
Answer: mp4
Correct!
Enter type of filename (#2) :
Answer: m
Incorrect!
Enter type of filename (#3) :
Answer: s
Incorrect!
No more questions are available.
您的代码中有许多小错误。
这是一个有一些改进的功能代码。它们在代码中写为注释。
import json
def do_game(jsonFile): #You need to put this function outside the main
for question in jsonFile["questions"]: # Use a for loop over the json list
filename = question["question"]
print(f"Enter type of filename '{filename}'") #Use F-String
answer = input("Answer: ")
print("Correct!" if answer==question["answer"] else "Incorrect!")
def main():
Total = None
with open("test.json", "r") as f:
jsonFile = json.load(f)
do_game(jsonFile) # Do all the game's code in a function
# It's a good practice to execute the main funciton only if this file is the main file
if __name__ == "__main__":
main()
由于您有问题列表,请在 json
中使用列表
{
"questions": [
{
"question": "Video.mp4",
"answer": "mp4"
},
{
"question": "cool.gif",
"answer": "gif"
},
{
"question": "main.py",
"answer": "python"
}
]
}
Main.py
在我现在的代码中,它只是停留在第一个问题上而没有继续第二个问题。我想让它循环直到回答最后一个问题并且循环将 break/stop.
def main():
Total = None
with open("test.json", "r") as f:
File = json.load(f)
for i in File["data"].keys():
Total = i # Total of the keys
def do_game():
num = 0
num += 1
if num > int(Total):
print("No more questions are available.")
return False # To break the loop but didn't work at all
name = File["data"][str(num)]["type"]
print("Enter type of filename (#{}) :".format(num))
answer = input("Answer: ")
if answer == name:
print("Correct!")
else:
print("Incorrect!")
while True:
do_game() # I tried to loop this but didn't go well
main()
test.json
{
"questions": {
"1": {
"question": "Video.mp4",
"answer": "mp4"
},
"2": {
"question": "cool.gif",
"answer": "gif"
},
"3": {
"question": "main.py",
"answer": "python"
}
}
}
尝试将循环调整为:
while True:
result = do_game()
if result == False:
break
你可以试试这个:
def main():
Total = None
with open("test.json", "r") as f:
File = json.load(f)
for i in File["data"].keys():
Total = i # Total of the keys
num = 1
while num <= int(Total):
name = File["data"][str(num)]["type"]
print("Enter type of filename (#{}) :".format(num))
answer = input("Answer: ")
if answer == name:
print("Correct!")
else:
print("Incorrect!")
num += 1
main()
因为你的数据是从1开始计数所以我们设置num = 1
.
您的代码根本就不是 运行 question
1 和 2(这是您数据的前 2 项)。它不会对文件进行任何循环。
您需要修改代码。试试这个,
import json
def main():
with open("test.json", "r") as f:
File = json.load(f)
total = len(File['questions'])
for i in range(1,total+1):
name = File["questions"][str(i)]["answer"]
print("Enter type of filename (#{}) :".format(i))
answer = input("Answer: ")
print("Correct!" if answer==name else "Incorrect!")
else:
print("No more questions are available.")
return False # To break the loop but didn't work at all
main()
输出:
Enter type of filename (#1) :
Answer: mp4
Correct!
Enter type of filename (#2) :
Answer: m
Incorrect!
Enter type of filename (#3) :
Answer: s
Incorrect!
No more questions are available.
您的代码中有许多小错误。
这是一个有一些改进的功能代码。它们在代码中写为注释。
import json
def do_game(jsonFile): #You need to put this function outside the main
for question in jsonFile["questions"]: # Use a for loop over the json list
filename = question["question"]
print(f"Enter type of filename '{filename}'") #Use F-String
answer = input("Answer: ")
print("Correct!" if answer==question["answer"] else "Incorrect!")
def main():
Total = None
with open("test.json", "r") as f:
jsonFile = json.load(f)
do_game(jsonFile) # Do all the game's code in a function
# It's a good practice to execute the main funciton only if this file is the main file
if __name__ == "__main__":
main()
由于您有问题列表,请在 json
中使用列表{
"questions": [
{
"question": "Video.mp4",
"answer": "mp4"
},
{
"question": "cool.gif",
"answer": "gif"
},
{
"question": "main.py",
"answer": "python"
}
]
}