YusufChowdhury
YusufChowdhury

Reputation: 349

Putting my coding in one line

I cannot get the following to function. I need validation by adding

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

However doing this on the code does not work:

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()

The loop breaks once adding the else loop for my validation.

Upvotes: 1

Views: 74

Answers (2)

asPlankBridge
asPlankBridge

Reputation: 1082

try

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

or

if number in [4, 6, 12]:

'6' is always true - so you always fulfil the condition... You also have to remove the quotation marks since you write number=int(...).

Upvotes: 2

gefei
gefei

Reputation: 19766

I don't see a loop, but one thing is wrong:

instead of

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

use

if number in (4, 6, 12):

Note that number has already been casted to int

Upvotes: 3

Related Questions