2014-01-15 26 views
1

我無法獲得以下功能。我需要驗證通過添加把我的編碼放在一行

else: 
     print("Type only 4, 6, or 12.") 

但是這樣的代碼不起作用:

def dicegame(): 
    #This defines the dicegame coding so I can call it later on 

    number=int(input("Hello. Enter either 4,6 or 12 to determine what sided dice you want to use ")) 

    import random 
    if number=="4" or "6" or "12": 
     print("You have rolled a", number, "sided dice with the number", random.randint(1,number)) 


    #These if loops direct the program to the correct coding down to what the user inputs (ie. 4, 6 or 12) 

    print("Would you like to roll again?") 
    answer=input("Enter either yes or no.") 

    if answer == ("yes"): 
     dicegame() 

    if answer == ("no"): 
     print("Ok, goodbye") 

    #Program prints "Would you like to roll again? 
    #Using input, user can reply to what the program prints, labelled with the variable 'answer' 

dicegame() 

循環中斷一旦加入別人環路我的驗證。

回答

3

我沒有看到一個循環,但有一點是錯誤的:

,而不是

if number=="4" or "6" or "12": 

使用

if number in (4, 6, 12): 

注意這個數字已經被強制轉換爲int

2

嘗試

if number==4 or number==6 or number==12: 

if number in [4, 6, 12]: 

「6」始終是真實的 - 所以你總是滿足以下條件:...... 你也有,因爲你寫number=int(...)刪除引號。