2016-08-30 56 views
0

我是新來的python,我無法弄清楚如何使這項工作,它只是不會工作。 (其他答案絕不會幫我)Python空閒3.5無效文字爲int()與基地10

這裏是我的代碼:

# My First Python Game Thing 
# Aka Endless Walking Simulator 
game_temp1 = True 
def choiceroom_thing1(): 
    while game_temp1: 
     directionchoice = input('L(left)R(Right)B(Backwards)F(Forwards)') 
     direction = int(directionchoice) 
     if direction in ['L', 'R','B', 'F']: 
      game_temp1 = False 
     if direction == "L": 
       print ('You went Left!') 
     if direction == "R": 
       print ('You went Right!') 
     if direction == "B": 
       print ('You went Backwards!') 
     if direction == "F": 
       print ('You went Forwards!') 
     game_temp1 = True 
     room_count = room_count + 1 
     choiceroom_thing1() 
print ('Hello Jeff...') 
print ('Heres my first game') 
print ("Adventure Game") 
input("Press Any Key To Continue...") 
room_count = 1 
while game_temp1: 
    directionchoice = input('L(left)R(Right)B(Backwards)F(Forwards)') 
    direction = int(directionchoice) 
    if direction in ['L', 'R','B', 'F']: 
     game_temp1 = False 
    if direction == "L": 
      print ('You went Left!') 
    if direction == "R": 
      print ('You went Right!') 
    if direction == "B": 
      print ('You went Backwards!') 
    if direction == "F": 
      print ('You went Forwards!') 
    game_temp1 = True 
    room_count = room_count + 1 
    choiceroom_thing1() 

,然後當我運行它,我得到這個錯誤。

Traceback (most recent call last): 
    File "C:/Users/Owner/Desktop/PythonProjects/Blah From Book.py", line 27, 
line 27, in <module> 
    direction = int(directionchoice) 
ValueError: invalid literal for int() with base 10: ':' 

有人可以修復我的代碼嗎?

+0

你問了一個字母作爲輸入,它不能被轉換爲'int' – gamda

+0

見鬼,爲什麼你叫'int'時你問用戶輸入'L','R','B'還是'F'? – user2357112

+0

然後我用什麼來取代int? –

回答

0

您不需要將directionchoice轉換爲int
direction應該等於directionchoice
然後測試,如果direction不想要的選擇:

if direction not in ['L', 'R','B', 'F']: 
相關問題