python程序循环,如何在Python程序中添加循环?

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值