网络安全小测验

2024-04-25 13:22:55 发布

您现在位置:Python中文网/ 问答频道 /正文

下面在python2.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

Tags: 代码inputmakeisonupperwhatscore
2条回答

首先,函数是upper,而不是Upper

其次,程序在python3中执行得很精确。你知道吗

如果您使用的是python2,则需要使用返回字符串的raw_input,而不是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应该是quit()

基于您的错误,您似乎正在使用python2。所以需要使用raw_input返回字符串。而是input。你还需要用upper来代替Upper看起来scorequit在这里是多余的!你知道吗

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

相关问题 更多 >