2017-01-22 38 views
-7

我正在嘗試創建一個RPG,在遊戲中玩家可以命名他們的角色。我遇到了一些麻煩,說它的名字是未定義的。我理解Python 2中的raw_input,這不是問題,我已經測試過,如果將'input'更改爲'raw_input',它根本不起作用,它只會結束程序。我想知道是否有人能幫我解決這個問題。如何輸入玩家名稱?

下面是我的代碼(我把我的名字從信貸對隱私的目的):

import random 

PN = 0 
Hit = 0 
health = 50 
level = 0 
EXP = 0 
nameless_variable_1 = 0 

print("Hello, and welcome to my creation!") 
print("To begin type 1") 
print("to see credits type 2") 
print("to quit go to the X button in the corner and click it") 
print("to continue type what you want to do and hit enter on your keyboard.") 
nameless_variable_1 = input("what is your choice??????") 

if nameless_variable_1 == 2: 
    print("................. The entire game") 
    print("do you want to play the game now?") 
    print("if you do press one and then hit enter") 
    nameless_variable_1 = input(" ") 

if nameless_variable_1 == 1: 
    PN = input("what is your character Name?") 
    print("it is the year 2019 and you are trying to assasinate an important person") 
    print("if you fail at this task, you can and probably will die.") 
+0

有什麼問題嗎? – MMF

+0

我正在尋求我的問題的幫助。 –

+1

你使用的是Python 2還是Python 3? – ImportanceOfBeingErnest

回答

0

在Python 2,raw_input將返回一個字符串,而input將返回一個字符串進行評估。因此input適用於數字,因爲eval('2')給出了2(作爲整數)。但它不適用於名稱,因此eval("John")會引發錯誤。

我會建議在整個代碼中使用raw_input。 對於數字,你可以再比較一個字符串 if nameless_variable_1 == "2":
和名稱它不會拋出一個錯誤。

+0

謝謝!這比我試圖做的更好! –