关于互联网安全的测验

时间:2014-12-30 11:39:42

标签: python python-2.7

以下在python 2.7中执行的代码给出了NameError

追溯是

answer1 = input("make a decision").Upper()
  File "<string>", line 1, in <module>
NameError: name 'C' is not defined 

完整的代码是。

print("This is a quiz on Internet safety!")
print("Question 1")
print("What is a firewall?")
print("A-A killer hot wall!")
print("B-A protection from virus'")
print("C-A protection from bad websites!")
answer1 = input("make a decision").Upper()
if answer1 == "C":
    print("Good!")
    score = score+1
else:
    print("You failed....")
    quit
print("Question 2")
print("What is a virus?")
print("A-A killer disease! RUN!")
print("B-A harmful bit of mumbo-jumbo on the computer")
print("C-A harmful programme which encrypts the computer as invalid")
answer2 = input("make a decision").Upper()
if answer2 == "B":
    print("Well Done!")
    score = score+1
else:
    print("You failed....")
    quit

2 个答案:

答案 0 :(得分:1)

首先,功能是upper而不是Upper

其次,程序完全在python3中执行。

我使用的是python 2,你需要使用返回字符串而不是raw_input的{​​{1}}。因此该行应

input

输出将是完美的。

answer1 = raw_input("make a decision").upper()

同样Question 1 What is a firewall? A-A killer hot wall! B-A protection from virus' C-A protection from bad websites! make a decisionC Good! 应为quit

答案 1 :(得分:0)

根据您的错误,您似乎正在使用python 2。所以你需要使用返回字符串的raw_input。而是input。您还需要使用upper代替Upperscorequit似乎是额外的!

score = 0
print("This is a quiz on Internet safety!")
print("Question 1")
print("What is a firewall?")
print("A-A killer hot wall!")
print("B-A protection from virus'")
print("C-A protection from bad websites!")
answer1 = raw_input("make a decision").upper()
if answer1 == "C":
    print("Good!")
    score = score+1
else:
    print("You failed....")
    quit