-1

I have this problem in my python code which is a coinflip game, the problem is that when It asks, "Heads or Tails?" and I just say 1 or Heads(same for 2 and Tails) without quotation marks and with quotation marks, it does not give me an answer that I am looking for.

I've Tried using quotation marks in my answer which didn't seem to work either.

import random

money = 100

#Write your game of chance functions here
def coin_flip(choice, bet):
   choice = input("Heads or Tails?")
   coinnum = random.randint(1, 2)
   if coinnum == 1:
      return 1
   elif coinnum == 2:
      return 2
   win = bet*2
   if choice == "Heads" or "1":
       return 1
   elif choice == "Tails" or "2":
      return 2
   if choice == coinnum:
      print("Well done! You have won " + str(win) + " Dollars!")
   elif choice != coinnum:
      print("Sorry, you lost " + str(bet) + " Dollars!")

coin_flip("Heads", 100)

The expected output was either "Well done! You have won 200 Dollars!" or "Sorry, you lost 100 Dollars!"

Da Ridg
  • 3
  • 4
  • Then do not return before printing. – Stop harming Monica Jun 11 '19 at 10:02
  • @DaRidg you need to start code with `\`\`\`` and end it with `\`\`\``. Then you can type normal code in between. More infos here: https://stackoverflow.com/editing-help#code – Finomnis Jun 11 '19 at 10:05
  • Regarding `if choice == "Heads" or "1":`, which doesn't do what you probably think it does, a good resource to read would be [How to test multiple variables against a value?](https://stackoverflow.com/questions/15112125/how-to-test-multiple-variables-against-a-value) – TrebledJ Jun 11 '19 at 10:06

1 Answers1

2

The first thing to note here is that your usage of return seems to be wrong. Please look up tutorials about how to write a function and how to use return.

I think this is what you were trying to do:

import random

money = 100

#Write your game of chance functions here
def coin_flip(choice, bet):
   choice = input("Heads or Tails? ")
   coinnum = random.randint(1, 2)
   win = bet*2

   if choice == "Heads" or choice == "1":
      choicenum = 1
   elif choice == "Tails" or choice == "2":
      choicenum = 2
   else:
      raise ValueError("Invalid choice: " + choice)

   if choicenum == coinnum:
      print("Well done! You have won " + str(win) + " Dollars!")
   else:
      print("Sorry, you lost " + str(bet) + " Dollars!")

coin_flip("Heads", 100)

Now, lets go through the mistakes I found in your code:

  • return was totally out of place, I wasn't sure what you were intending here.
  • if choice == "Heads" or "1" is invalid, "1" always evaluates to true. Correct is: if choice == "Heads" or choice == "1":
  • elif choice != coinnum: is unnecessary, if it doesn't run into if choice == coinnum: a simple else: would suffice.
Finomnis
  • 18,094
  • 1
  • 20
  • 27
  • for some reason in vscode, it says "IndentationError: unexpected indent" with "else:" – Da Ridg Jun 11 '19 at 10:08
  • 1
    Can't help you with that, I think you are doing something wrong there :/ https://onlinegdb.com/B1R60epAE – Finomnis Jun 11 '19 at 10:10
  • sorry i changed the comment – Da Ridg Jun 11 '19 at 10:12
  • I still think you are doing something wrong ... It works flawlessly for me. Be sure to copy all the indentation, python indentation is not just for looks – Finomnis Jun 11 '19 at 10:15
  • it seems to work on any online python runner but not in vscode – Da Ridg Jun 11 '19 at 10:17
  • Also, while it looks the same, *tab* and *multiple spaces* are **not** the same thing in python. Please make sure you don't have some of those mixed up. – Finomnis Jun 11 '19 at 10:17
  • File "", line 1 /Users/myname/Documents/Coding/Python/chancegames.py ^ SyntaxError: invalid syntax >>> – Da Ridg Jun 11 '19 at 10:21
  • That again is a different error than the "IndentationError" you told us earlier. Make up your mind :P – Finomnis Jun 11 '19 at 10:22
  • Yeah sorry the original error came up again saying syntax error – Da Ridg Jun 11 '19 at 10:23
  • I'm not sure if this is something that we can really fix in the comment section ... This goes more in the direction of *how to use your ide* and is therefore off-topic. Maybe ask someone who can come over and explain more interactively, or some remote desktop session or something? – Finomnis Jun 11 '19 at 10:25
  • 1
    Sorry about all this confusion, I fixed it(I restarted vscode). Thx for the help :) – Da Ridg Jun 11 '19 at 10:28