It won't work!

def get_user_choice():
    from string import digits
    user_choice = input('Type a number, 1 to 6.\n\n')
    if user_choice == '':
        print 'Not a valid choice.'
        raw_input()
        main()
    check_if_true(user_choice)

def check_if_true():
    import random
    
    comp_choice = random.randint(1,6)
    
    if comp_choice != userchoice:
        print 'The computer won :('
        return comp_choice, user_choice
    
    else:
        print 'You Won!'
        return user_choice, comp_choice
        raw_input()
        main()
        

def main():
    get_user_choice()
    
    return

while 1:
    main()

Recommended Answers

All 9 Replies

If you call a function like this
check_if_true(user_choice)
and you use variable user_choice in that function
then you have to define the function like this
def check_if_true(user_choice):
to get the argument passed.

A little messy this.
Import <moudule> always first not inside code.
You call main() 3 times,call main() one time.
if comp_choice != userchoice: type error.
And the big mistake vegaseat pointet out.

It won't work!

Yes that`s for sure true.

The logic could be better,user only win if 3-3 4-4....
Maybe hightest dice number should win.
So the script without function,with a different print statement.

import random

user_choice = input('Type a number, 1 to 6.\n\n')
comp_choice = random.randint(1,6)

if comp_choice != user_choice:
    print 'Computer got %d user had %d | Computer won' % (comp_choice,user_choice)   
else:
    print 'Computer got %d user had %d | User won' % (comp_choice,user_choice)

Now think off again how to use function for this code.
Here just to give you and idèe off one way to set it upp.

def get_user_choice():
   print 'your code'
   pass

def check_if_true():    
    pass

def main():
    '''Main menu and info ''' 
    while True:               
        print 'Welcome to my menu\n'
        print '(1) Throw a dice against computer'
        print '(2) Nothin yet'
        print '(q) Quit' 
        choice = raw_input('Enter your choice: ') 
        if choice == '1':
            get_user_choice()           
        elif choice == '2': 
            pass 
        elif choice == 'q': 
            exit() 
        else: 
            print 'Not a correct choice:', choice 

if __name__ == '__main__':
    main()

my new code:

def get_user_choice():
    from string import digits
    user_choice = input('Type a number, 1 to 6.\n\n')

    #i want an if statement here to check if number equals 7-maximum (which is why i imported digits here)

    if user_choice == 'q':
        quit()
        
    elif user_choice == '':
        print 'Not a valid choice.'
        raw_input()
        get_user_choice()
        
    check_if_true(user_choice)

def check_if_true(user_choice):
    import random
   
    comp_choice = random.randint(1,6)
   
    if comp_choice == user_choice:
        print 'You Guessed Right!'
        print user_choice, 'to', comp_choice
        raw_input()
        get_user_choice()

    else:
        print 'The computer won :('
        print comp_choice, 'to', user_choice
        raw_input()
        get_user_choice()
      
    

       

def main():
    print '(s): Start round'
    print '(q): Quit program'
    print
    
    user_input = input('Enter letter here --> ')

    if user_input == 's':
        get_user_choice()

    elif user_input == 'q':
        quit()

    else:
        user_input = input('You Must Enter a Letter --> ')
      
   

if __name__ == '__main__':
    main()

You have to study and learn the basic of function,now it`s no god.
Run this the first function and understand what is dos.
Here i make sure that number are between 1-6.

import random  #import shall alway be first

def get_user_choice():   
    while True:
        try:          
            user_choice = int(raw_input('Type a number, 1 to 6: '))
            if user_choice <=6 and user_choice >= 1:
                print user_choice   #test print                
                #return user_choice #we return and this exit this function when number is between 1-6
                break  #we break out off loop.                         
            else:
                print 'Numbers most be between 1 and 6'       
        except ValueError:
            print 'Only numbers'    

get_user_choice()

Now with return.
Think of how this work,now this function is returning user_choice.
Then this function has done it`s work

Then you catch that variable in your next function.
To do more work with variable user_choice
def check_if_true( user_choice ):

def get_user_choice():   
    while True:
        try:          
            user_choice = int(raw_input('Type a number, 1 to 6: '))
            if user_choice <=6 and user_choice >= 1:                              
                return user_choice #we return and this exit this function when number is between 1-6
            else:
                print 'Nubmer most be between 1 and 6'       
        except ValueError:
            print 'Only numbers'

Okay so returning signals the end of a function as well as confirms the functions ability to pass a variable made in the function?

So....i was right about my last post?

I didn't really find the answer but i can't really think of anything more logical than what my last post said.

Okay so returning signals the end of a function as well as confirms the functions ability to pass a variable made in the function?

Yes, a return statement exits the function and passes a value back to the point in code that called it. You can return any object no matter how complex. By omitting a value and just using return you are returning a None type object (ie, nothing).

Yes, a return statement exits the function and passes a value back to the point in code that called it. You can return any object no matter how complex. By omitting a value and just using return you are returning a None type object (ie, nothing).

Oh my god thats just what i wanted to hear! the other programming language i used has similar functions to this in which im good at :) yay

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.