如何在Python程序中添加循环? [英] How do I add a loop to a Python program?

查看:118
本文介绍了如何在Python程序中添加循环?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在试图弄清楚如何让程序一遍又一遍地运行,但我不知道。有人可以帮忙吗?谢谢! :/这是代码:

print('选择答案,在答案前输入数字!享受!请给我们一些关于如何改进的反馈!')



person =输入('输入你的名字:')

print'Hello',person



import sys

随机导入



class问题(对象):

def __init __(自我,问题,答案,选项):

self.question =问题

self.answer =回答

self.options = options



def ask(self):

print self.question +?

for n,enumerate中的选项(self.options) ):

打印%d)%s%(n + 1,选项)



response = int(sys.stdin。 readline()。strip())#answers是整数

if response == self.answer:

printCORRECT

else:

打印WRON G,再试一次



问题= [

问题(马上有多少条腿,4,[一 ,两个,三个,四个,五个],

问题(自行车上有多少个轮子,2,[一个,两个, 三,二十六],

问题(字母表中有多少个字母,3,[二十四,二十二,二十六, 二十八],

问题(谁赢得了今年的超级碗,2,[海鹰,爱国者,包装工,乌鸦]), />
问题(谁是美国国家橄榄球联盟年度最佳新秀,1,[奥德尔贝克汉姆少年,迈克埃文斯,杰里米希尔]),

问题(谁是美国的第一任总统,2,[约翰亚当斯,乔治华盛顿,詹姆斯麦迪逊]),

问题(多少码头在国家橄榄球联盟体育场,3,[九十,八十,百]),

#更详细的格式

问题(问题=什么颜色是天鹅在澳大利亚,

答案= 1,

选项= [黑色,白色,粉红色,绿色]),#最后一个也可以有一个逗号

]



random.shuffle(问题)#随机化问题的顺序



问题中的问题:

question.ask()

print('谢谢你玩!想再玩一次?只需再次运行这个程序!')

解决方案

对于阅读原始文档的基本问题是否有效是不是很明显,但问这样的问题是不是?

请参阅:

https://docs.python.org/ 2 / tutorial / introduction.html [ ^ ],

https://docs.python.org/2/tutorial/ controlflow.html [ ^ ]。



这只是介绍中的主题。在编写任何代码之前,您必须阅读整本手册并至少浏览参考资料,至少知道在哪里可以找到每个细节:

https://docs.python.org/2/reference/index.html [ ^ ]。



特别是,与你的相关问题;

https://docs.python.org/2/reference/ compound_stmts.html [ ^ ],

https://docs.python.org/2/ reference / compound_stmts.html #the-while-statement [ ^ ],

https://docs.python.org/2/reference/compound_stmts.html#the- for-statement [ ^ ]。



依旧......



-SA

I'm trying to figure out how to make a program run over and over and over again, but I don't know. Could someone help? Thanks! :/ Here is the code:
print('To select an answer, type the number before the answer! Enjoy! Please give us some feedback on how to improve!')

person = input('Enter your name: ')
print'Hello', person

import sys
import random

class Question(object):
def __init__(self, question, answer, options):
self.question = question
self.answer = answer
self.options = options

def ask(self):
print self.question + "?"
for n, option in enumerate(self.options):
print "%d) %s" % (n + 1, option)

response = int(sys.stdin.readline().strip()) # answers are integers
if response == self.answer:
print "CORRECT"
else:
print "WRONG, TRY AGAIN"

questions = [
Question("How many legs are on a horse ", 4, ["one", "two", "three", "four", "five"]),
Question("How many wheels are on a bicycle", 2, ["one", "two", "three","twenty-six"]),
Question("How many letters are in the alphabet", 3, ["twenty four", "twenty two", "twenty six", "twenty eight"]),
Question("Who won the Super Bowl this year", 2,["Seahawks","Patriots", "Packers", "Ravens"]),
Question("Who was the Offensive Rookie of the Year for the National Football League", 1, ["Odell Beckham Junior", "Mike Evans", "Jeremy Hill"]),
Question("Who was the first president of the United States", 2, ["John Adams", "George Washington", "James Madison"]),
Question("How many yards are in a National Football League stadium", 3, ["ninety", "eighty", "hundred"]),
# more verbose formatting
Question(question="What colour is a swan in Australia",
answer=1,
options=["black", "white", "pink", "green"]), # the last one can have a comma, too
]

random.shuffle(questions) # randomizes the order of the questions

for question in questions:
question.ask()
print('Thank you for playing! Want to play again? Simply run this program again!')

解决方案

Isn't it obvious that with elementary issues reading original documentation is efficient, but asking such question is not?
Please see:
https://docs.python.org/2/tutorial/introduction.html[^],
https://docs.python.org/2/tutorial/controlflow.html[^].

That's just the topic in introduction. Before writing any code, you have to read the whole manual and at least scan through the reference, at least to know where you can find every detail:
https://docs.python.org/2/reference/index.html[^].

In particular, as related to your "problem";
https://docs.python.org/2/reference/compound_stmts.html[^],
https://docs.python.org/2/reference/compound_stmts.html#the-while-statement[^],
https://docs.python.org/2/reference/compound_stmts.html#the-for-statement[^].

And so on…

—SA


这篇关于如何在Python程序中添加循环?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆