Cpal
Cpal

Reputation: 1

I run this python program in cmd and nothing happens

#These 2 imports are for functions used below.

from time import sleep
from random import randint

print("Starting up...")

def maxNum():
    max_int = 6
    print("The maximum number to guess is %d" % max_int)

def userGuess():
    guess1 = int(input("Guess what number you think the dice will land on? \r>"))


if guess1 > max_int:
    print("You chose a number higher than the maximum number allowed!")
else:
    print("Rolling...")
    sleep(1)
    print("The number is...")
    sleep(2)
    print(randint(1,6))

This is meant to be a game where the user guesses a number that the die will land on, and then a random number from 1 to 6 will print. I don't know what's wrong with it and why nothing will happen when i run it in cmd (im on windows 7). Any help?

Upvotes: 0

Views: 594

Answers (3)

Nik M
Nik M

Reputation: 25

I'm sure this has been answered before...

Anyways, this worked:

from random import randint

max_int = 6
number = randint(1, 6)

while True:
    print("Guess what number the dice will land on.")
    guess = input()
    guess = int(guess)

    if guess < number:
        print("too low")

    if guess > number:
        print("too high")

    if guess == number:
        break

if guess == number:
    print("You guessed correctly")

Upvotes: 0

Panto
Panto

Reputation: 104

First of all, you need to call the functions, as mentioned earlier. I also think it makes sense to call maxNum() from inside userGuess(), so you only have to call userGuess() at the bottom.

Second, max_int is only defined whitin the maxNum() function, so userGuess() will not have access to that and you will get an error.

So, something like this:

from time import sleep
from random import randint

print("Starting up...")

def maxNum(max_int):
    print("The maximum number to guess is %d" % max_int)

def userGuess():
    max_int = 6
    maxNum(max_int)
    guess1 = int(input("Guess what number you think the dice will land   on?  >"))

    if guess1 > max_int:
          print("You chose a number higher than the maximum number   allowed!")
    else:
          print("Rolling...")
          sleep(1)
          print("The number is...")
          sleep(2)
          print(randint(1,6))

userGuess()

Upvotes: 0

Taohidul Islam
Taohidul Islam

Reputation: 5414

You have define the functions but didn't call those. You need to call the functions ,like:

maxNum()
userGuess()

Upvotes: 4

Related Questions