0

I've looked around for a question like this. I've seen similar questions, but nothing that really helped me out. I'm trying to pass the choice variable from the method rollDice() to the method main(). This is what I've got so far:

import random
import os
import sys

def startGame():
     answer = input('Do you want to play Super Dice Roll?\nEnter 1 for Yes\nEnter 2 for No\n'
     os.system('cls')
     if (answer == '1'):
          rollDice()
     elif(answer == '2'):
          print('Thank you for playing!')
     else:
          print('That isn/t a valid selection.')
          StartGame()

def rollDice():
     start = input('Press Enter to roll dice.')
     os.system('cls')
     dice = sum(random.randint(1,6) for x in range (2))
     print('you rolled ',dice,'\n')
     choice = input('Do you want to play again?\nEnter 1 for Yes\nEnter 2 for No.\n)
     return choice

def main():
     startGame()
     while (choice == '1'):
          startGame()
     print('Thank you for playing')

print('!~!~!~!~WELCOME TO SUPER DICE ROLL~!~!~!~!~\n')
main()

I know that I may have other things in here that are redundant or I may have to fix, but I'm just working on this one issue right now. I'm not sure how to pass the choice variable into the main() method. I've tried putting choice == rollDice() in the main() method but that didn't work. I do mostly SQL work, but wanted to start learning Python and I found a website that has 5 beginner tasks but virtually no instructions. This is task one.

3
  • your input (for rollDice) is missing a ' Commented Oct 19, 2017 at 15:27
  • I'd recommend another site if this is task 1 and there is no instruction Commented Oct 19, 2017 at 15:31
  • @depperm, yes you're right. The code is on another machine and I was just typing it out and missed that part. However, I do have that included. Yeah, I understand trying another site. I just felt like the more research I had to do the more I'd learn. Commented Oct 19, 2017 at 15:42

1 Answer 1

1

You need to put the return value of the function into a variable to be able to evaluate it (I also corrected a few bugs in your code, mainly typos):

import random
import os

def startGame():
    answer = input('Do you want to play Super Dice Roll?\nEnter 1 for Yes\nEnter 2 for No\n')
    os.system('cls')
    while answer == '1':
        answer = rollDice()
    if answer == '2':
        print('Thank you for playing!')
    else:
        print('That isn/t a valid selection.')
        startGame()

def rollDice():
    input('Press Enter to roll dice.')
    os.system('cls')
    dice = sum(random.randint(1,6) for x in range (2))
    print('you rolled ', dice, '\n')
    choice = input('Do you want to play again?\nEnter 1 for Yes\nEnter 2 for No.\n')
    return choice

def main():
    print('!~!~!~!~WELCOME TO SUPER DICE ROLL~!~!~!~!~\n')
    startGame()
    print('Thank you for playing')


main()
Sign up to request clarification or add additional context in comments.

5 Comments

Thank you for your answer. This leads me to another question: If I declare that choice = '1' in the main() then won't it always end up as '1' no matter what the user inputs? What if the user chooses not to play again and selects '2'?
The choice='1' is just the default value to get the game started for the first time. in choice = startGame() the variable is overwritten with the actual user input which is then used for the next time the while loop is run.
I you do like my answer I would appreciate if you could accept it (the check mark below the vote button). Thank you.
Ok. I tried that out and I had to debug. When I ran it step by step it followed fine until I chose '2' as the user input. It chose the correct path of the IF statement and then when it printed 'Thank you for playing!' and it stored '2' for choice. But then it went back to the while loop and checked the value of choice and determined that choice was equal '1'. I'm not sure if it passed the input value to the main. I'm thinking that maybe because it didn't pass the value '2' that when it went back to the main it kept that instance of choice that it already knew at '1'?
@AP1: I have updated my answer. Now it works as you would expect it to. The structure before was too complicated for this task. So I simplified it even more.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.