Game of Chance in Python 3.x?

2024/7/6 21:20:48

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 randommoney = 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 1elif coinnum == 2:return 2win = bet*2if choice == "Heads" or "1":return 1elif choice == "Tails" or "2":return 2if 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!"

Answer

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 randommoney = 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*2if choice == "Heads" or choice == "1":choicenum = 1elif choice == "Tails" or choice == "2":choicenum = 2else: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.
https://en.xdnf.cn/q/119714.html

Related Q&A

Count occurence of a word by ID in python

Following is the content of a file,My question is how to count the number of occurences for the word "optimus" for different IDs ID67 DATEUID Thank you for choosing Optimus prime. Please w…

ModuleNotFoundError: No module named plyer in Python

I am trying to write a program notify.py (location: desktop) that uses plyer library to get a notification on windows 10. I used pip install plyer and am using vs code to run the program but I get an e…

Floating point to 16 bit Twos Complement Binary, Python

so I think questions like this have been asked before but Im having quite a bit of trouble getting this implemented. Im dealing with CSV files that contain floating points between -1 and 1. All of thes…

Flag the first non zero column value with 1 and rest 0 having multiple columns

Please assist with the belowimport pandas as pd df = pd.DataFrame({Grp: [1,1,1,1,2,2,2,2,3,3,3,4,4,4], Org1: [x,x,y,y,z,y,z,z,x,y,y,z,x,x], Org2: [a,a,b,b,c,b,c,c,a,b,b,c,a,a], Value: [0,0,3,1,0,1,0,5,…

How to split up data from a column in a csv file into two separate output csv files?

I have a .csv file, e.g.:ID NAME CATEGORIES 1, x, AB 2, xx, AA 3, xxx, BAHow would I get this to form two output .csv files based on the category e.g.:File 1:ID NAME CATEGORY 1, x, A 2, xx, A 3, …

Discord.py spellcheck commands

Recently, I looked up Stack Overflow and found this code which can check for potential typos: from difflib import SequenceMatcher SequenceMatcher(None, "help", "hepl").ratio() # Ret…

Django Model Form doesnt seem to validate the BooleanField

In my model the validation is not validating for the boolean field, only one time product_field need to be checked , if two time checked raise validation error.product_field = models.BooleanField(defau…

For loop only shows the first object

I have a code that loops through a list of mails, but it is only showing the first result, even though there are also other matches. The other results require me to loop over the mails again only to re…

IndexError: pop from empty list

I need help. I have no idea why I am getting this error. The error is in fname = 1st.pop()for i in range(num) :fname = lst.pop()lTransfer = [(os.path.join(src, fname), os.path.join(dst, fna…

Cannot import name StandardScalar from sklearn.preprocessing [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.This question was caused by a typo or a problem that can no longer be reproduced. While similar q…