Reputation: 17
Just started to code, can't figure out why the start
function will not ask me for any user input.
import random
number = random.randint(1,6)
def start():
print("Do you want to start?")
answer = raw_input("Type 'Yes' to start!") #Asks me for user input... Doesn't work
if answer == "yes":
print("Rolling the dice... The number is... " + number)
else:
print("Aww :( ")
Upvotes: 0
Views: 108
Reputation: 1695
You want to give consideration to your program as a module.
Try making the following changes:
import random
_NUMBER = random.randint(1,6)
def start():
print("Do you want to start?")
answer = raw_input("Type 'Yes' to start!")
if answer == "yes":
print("Rolling the dice... The number is... " + _NUMBER)
else:
print("Aww :( ")
if __name__ == "__main__":
start()
The "if name"... addition allows your program to be run from the interpreter as a module. You can also import this module into other programs easily.
I've also changed the syntax on your global variable (number) to reflect best practices. It's now private -- indicated by the underscore -- and uppercase.
If this program is imported as a module, the global variable won't impact those of the same name.
Now you can do python filename.py
from the command line, or from filename import start
and start()
from the interpreter to run your program.
Upvotes: 3
Reputation: 533
Just like the other two said, you haven't called the function "start()". Also you asked to type "Yes" but you check if the user gave "yes".
Upvotes: 2
Reputation: 884
You have to call the function.
start()
Working script:
import random
number = random.randint(1,6)
def start():
print("Do you want to start?")
answer = raw_input("Type 'Yes' to start!")
if answer == "yes":
print "Rolling the dice... The number is... ", number
else:
print("Aww :( ")
start()
Upvotes: 2
Reputation: 16711
You never actually call the function:
number = random.randint(1,6)
def start():
print("Do you want to start?")
answer = raw_input("Type 'Yes' to start!")
if answer == "yes":
print("Rolling the dice... The number is... " + number)
else:
print("Aww :( ")
start()
Upvotes: 2