运行while循环时继续获取ValueError

时间:2014-10-08 10:38:39

标签: python python-3.x

我的代码在运行无限while循环时向我提供了错误“ValueError:需要多于1个值来解包”,并且在获取问题之前,直到用户输入no来打破它。任何线索1更多的价值是指?

from random import shuffle
questions = [
("Which organization develops the 802 family of standards for wired and wireless LANs and     MANs?", "ieee"),
("What type of delivery uses data link layer addresses?", "local delivery"),
    ("What organization developed the OSI reference model that is used in networking?", "iso"),
    ("Which message delivery option is used when all devices need to receive the same message simultaneously?", "broadcast"),
    ("Which type of network design combines voice, video, and data on the same communication channel?", "converged"),
    ("During a routine inspection, a technician discovered that software that was installed on a computer was secretly collecting data about websites that were visited by users of the computer. Which type of threat is affecting this computer?", "spyware"),
    ("Which device acts as a gateway to allow hosts to send traffic to remote IP networks?", "local router"),
    ("What will a network administrator use to modify a configuration on a Cisco router?", "ios"),
    ("To save time, IOS commands may be partially entered and then completed by typing which key or key combination?", "tab"),
    ("An administrator measured the transfer of usable data across a 100 Mb/s physical channel over a given period of time and obtained 60 Mb/s. Which kind of measurement did the administrator obtain?", "goodput"),
]
shuffle (questions)
numRight = 0
numQuest = 0
wrong = []
print ("Welcome to my computer networking Quiz, based on Cisco material.")
while True:
    for questions, rightAnswer in questions:
        answer = input(questions + " ")
        if answer.lower() == rightAnswer:
            print("Congratulations, that is the right answer!")
            numRight += 1
            numQuest += 1        
            answer2 = input("Would you like to continue? Type yes or no: ")        
            if answer2.lower() == "no":
                break
        else:
            print("That is the wrong answer my friend!")
            numQuest += 1
            answer2 = input("Would you like to continue? Type yes or no: ")
            if answer2.lower() == "no":
                break
a = numRight
b = numQuest
def stats(a,b):
    return a/b*100
if stats(a,b) >= 60.0:
    print("You got", stats(a,b), "percent right. You pass!")
else:
    print("You got", stats(a,b), "percent right. You fail!")

1 个答案:

答案 0 :(得分:2)

你的问题很简单:"打破"在循环中调用只是"打破"循环遍历for,但不会过去。 为了解决这个问题,你只需要保存一个变量" break_while",将其初始化为false并将其用作while的条件。当用户回答" no"然后将此变量设置为True,而while将停止:

 while (not break_while):

      ***

      if answer2.lower() == "no":
          break_while = True
          break

编辑:仔细查看你的代码,你必须小心使用两个不同的同名变量,即"问题"

for questions, rightAnswer in questions:
    ***

你得到这个错误是因为在使用break之后,while仍然处于活动状态并且for句子被再次调用,但是使用变量"问题"改为一个特定的问题,这不是一个元组,因此引发了ValueError。 更改第一个"问题的名称"到"问题"为了避免这种冲突。