-2
dicegame=input("wanna play dice? yes/no : ")
userdice1=random.randint(1,6)
userdice2=random.randint(1,6)
pythondice1=random.randint(1,6)
pythondice2=random.randint(1,6)
print("you got", userdice1,"and", userdice2)
print("and i got",pythondice1,"and",pythondice2)
if userdice1+userdice2==pythondice1+pythondice2:
    print("its a tie!")
if userdice1+userdice2<pythondice1+pythondice2:
    print("i won!")
if userdice1+userdice2>pythondice1+pythondice2:
    print("you won!, great job")
dice=input("wanna play again? : ")
if input=="yes":
    print(dicegame)
if input=="no":
    print("cya later.")

#its supposed to repeat dicegame if you input yes, but after i input yes it just says process finished with exit code 0

mishuf
  • 1
  • if you want dicegame to repeat, you should convert that part of the code to a function or you should put that part of the code into a loop and control the loop. Read more about [loops](https://www.w3schools.com/python/python_for_loops.asp) – Joe Ferndz Dec 27 '20 at 23:25
  • 1
    The words "run again" are a hint that you need a loop in your code. Learn about loops. – RufusVS Dec 27 '20 at 23:36
  • Does this answer your question? [How do I repeat the program in python](https://stackoverflow.com/questions/41365922/how-do-i-repeat-the-program-in-python) – mkrieger1 Dec 27 '20 at 23:39
  • Does this answer your question? [How do I restart a program based on user input?](https://stackoverflow.com/questions/14907067/how-do-i-restart-a-program-based-on-user-input) – Gino Mempin Dec 27 '20 at 23:40

3 Answers3

0
  1. If you want to repeat the game you have to put the code inside a loop.
  2. When you ask the user, if he wants to play again, you store the answer in the variable dice. However you try to access it through input afterwards. I assume you meant to use dice but just in case: Do not use input as a variable name, that's bad practice.

Try the following:

    import random
    
    dicegame=input("wanna play dice? yes/no : ")
    while dicegame=='yes':
        userdice1=random.randint(1,6)
        userdice2=random.randint(1,6)
        pythondice1=random.randint(1,6)
        pythondice2=random.randint(1,6)
        print("you got", userdice1,"and", userdice2)
        print("and i got",pythondice1,"and",pythondice2)
        if userdice1+userdice2==pythondice1+pythondice2:
            print("its a tie!")
        if userdice1+userdice2<pythondice1+pythondice2:
            print("i won!")
        if userdice1+userdice2>pythondice1+pythondice2:
            print("you won!, great job")
        dicegame=input("wanna play again? yes/no: ")

Changes:

  • Put the whole thing in a loop.
  • Renamed the variable dice from the last fifth last line to 'dicegame'
  • Changed input (used like a variable name) also to dicegame.
Syntarctic
  • 66
  • 5
  • thank you, i was really confused earlier and this helped a lot – mishuf Dec 28 '20 at 00:07
  • @mishuf, please choose the answer that you consider the most helpful and mark it as "the answer". That way stackoverflow knows the problem has been solved. – Syntarctic Dec 29 '20 at 09:51
0

While the other answers are supperior in terms of raw code, I get the feeling you are only starting out with Python. So ill try and make this as approachable as possible. For starters, I think you may be getting tripped up on what functions actually are.

input is a function, not a value. (If you ever studied maths, think of it like f(x) or sin(x)/cos(x)/tan(x)). All the functions do are let you pass in a number, and get a different number back out. While a little more complex, input is no different.

While this video is fairly degrading to watch, it gets the point across: https://www.youtube.com/watch?v=ZEsCla92mek

Thinking about the function as a machine (put raw materials into it, the machine changes it, and then spits something useful out), the statement if input=="yes" is asking the computer if the machine is equal to "yes", and not whether the output of the machine is equal to "yes".

In terms of solving the error in your code, you may want to learn about while loops. This keyword tells a computer to keep doing the same action until a function is met. For example (using some of your own variables), while (userdice1 != 1): userdice1 = random.randint(1,6) won't stop generating a new number until the condition is met, i.e., until userdice1 == 1.

In addition, you should have a search online about the break keyword, which lets you escape from a loop; there are plenty of resources out there.

If you can handle those 3 points, you will be easily able to tackle this problem

Sovereign
  • 53
  • 4
0

There are a few problems in the code you posted; I'll make some suggestions for you.

First off, when the user inputs their answer to whether they want to play again or not, the code will not detect this because you used a different variable name instead of dice.

Next, your line dicegame=input("wanna play dice? yes/no : ") does not really do anything because the variable you set is never used again.

Finally, you should organize your game code into a function and then put all of it into a while loop that can run over and over if the user keeps inputting 'yes'. A main function can be used to organize this nicely as well, you should always use one of those.

An working example of your game could be:

import random

def dice_game():
    userdice1 = random.randint(1,6)
    userdice2 = random.randint(1,6)
    pythondice1 = random.randint(1,6)
    pythondice2 = random.randint(1,6)
    print("you got", userdice1, "and", userdice2)
    print("and i got",pythondice1,"and",pythondice2)
    if userdice1 + userdice2 == pythondice1 + pythondice2:
        print("its a tie!")
    elif userdice1 + userdice2 < pythondice1 + pythondice2:
        print("i won!")
    elif userdice1 + userdice2 > pythondice1 + pythondice2:
        print("you won!, great job")

def main():
    # set boolean value for whether to run the game or not
    run_game = True
    while run_game:
        # run the dice game
        dice_game()
        # ask the user if they want to play again
        dice = input("wanna play again? : ")
        if dice == "yes":
            # run the game again
            run_game = True
        elif dice == "no":
            # don't run the the game again
            run_game = False
            print("cya later.")

main()
bperry14
  • 55
  • 6
  • thank you, i took some things into consideration and its all working now, thanks again! – mishuf Dec 28 '20 at 00:07
  • Just a reminder, instead of saying thanks under a useful post, just choose it as your answer for your question or upvote it to help everyone know you got it figured out! – bperry14 Dec 28 '20 at 00:10