-1

The question has been answered but I have to wait 7 minutes to mark it.

I'm trying to make a coin flip program in Python, using the random library. When the program is run, I input the names for headsplr, tailsplr and mode, but after entering the mode the program immediately crashes. I suspect it is something to do with the slashes in the input message.

I tried using a built-in library called 'pdb', which debugs the program, but it gave me this <string>(1)<module>(), which I have no idea what it means.

import random

headsplr = input("Type the player using Heads.\n")
tailsplr = input("Type the player using Tails.\n")
mode = input("Enter the mode you want to play. Modes: classic <-(50/50) or unbalanced <-(51/49)\n")

if str(mode) == "classic":
    equalchance = random.randint(0,1) # 0 is Heads and 1 is Tails
    if equalchance == "0":
        print("Heads won!")
    elif equalchance == "1":
        print("Tails won!")
Skelebrine
  • 3
  • 1
  • 2

2 Answers2

2

From what I can see, the program does not crash but it just doesn't output anything. That's because you're comparing equalchance to strings instead of integers, which is what random.randint() returns. You should compare equalchance == 0 or equalchance == 1, removing the quotes.

Shinra tensei
  • 1,283
  • 9
  • 21
  • It worked! Thank you. I can mark the answer correct in 9 minutes. – Skelebrine Aug 12 '21 at 08:55
  • 2
    @Skelebrine By the way, I want you to notice how important it is to differentiate between the code "crashing" and "not working as I expected". If it crashes, you should see a stack trace, but if you don't your code is not crashing. This might sound not very relevant but you might confuse people who want to help you (read the first comment in the question) – Shinra tensei Aug 12 '21 at 08:59
  • Sorry about that. I'm very new to Python and have no experience. I had trouble wording the description. – Skelebrine Aug 12 '21 at 09:02
  • 1
    Side note: in `if str(mode) == "classic":` you don't need the call to `str`. Since `mode` is the return value of a call to the `input` function it is already a string. A simple `if mode == "classic":` is enough. – Matthias Aug 12 '21 at 09:16
  • @Skelebrine it's all right, that's for the next time, welcome to StackOverlow – Shinra tensei Aug 12 '21 at 09:16
2

Randint returns an int object, and you compare it to a string.

# replace this:
if equalchance == "0":

# by this
if equalchance == 0:

# and same for the second condition.
klegoff
  • 51
  • 3