2014-12-30 38 views
-3

在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

如果不工作,那麼是的,代碼是錯誤的。 – JJJ

+0

請更正您的縮進。然後告訴最新的腳趾問題和你的Python版本是什麼? – Kasramvd

+0

比「似乎沒有工作」更具體。 – interjay

回答

1

首先功能是upper和不Upper

其次節目恰好python3執行。

我你使用的是Python 2,你需要使用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()

+1

謝謝! IT似乎現在工作! – TheHolyTurnip

+0

歡迎您 –

+1

請注意,quit()(或exit())不應用於生產代碼(RTFM)。它具有關閉sys.stdin(可能是意想不到的)副作用,所以僅僅捕獲SystemExit是不夠的。這在這裏沒有什麼不同,但這是不好的做法。 – cdarke

0

根據你的錯誤,你似乎在使用python 2。所以你需要使用返回字符串的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