All I can say is, great job. You’ve learned to tie your shoelaces. Now the real task is to refactor the code. Can you pare down the logic and control to something modular and recognizable? That would be without video support.
Not sure what you mean by video support…this project was for people that finished the first 3 sections of Codecademy, so I did it with the knowledge I have so far.
Ah, so there is no video? Okay, my bad. Still, with what you know can you find ways to refactor your code? I can, without going beyond what concepts are present. Bookmark this exercise and return to it in a couple of weeks.
my game of chances project.
any feedback would be appreciated.
Only suggestion I have is to update your format method. The more pythonic method is such as print(f’words and a {variable_name}’)
Here is what I came up with. I tried to go a little further and make a somewhat complete game.
https://github.com/amorydoerr/chance-games
This is what I’ve done, I used a bit help from this forum but, in any case, I learnt quite a lot thanks to this exercise.
Still a lot to improve though…
My effort:
Check out my version! It’s been made with classes instead of simple functions. Try to break it!
I like possibility to make many bets on roulette at one turn. I’ll try to add it to my proj too.
Hi Everyone!
Here is my code for the project: https://gist.github.com/nktnlx/ce0522ba4971b562d6bfbac3abc06193
The most challenging part for me was to make a tie in the Cards game. Solved by creating a nested list and its consequent indexing.
Any comments or feedback are welcomed!
Cheers!
Interactive input-console version
hey. noob here. thanks for the time and help. you guys are awesome!
i’ve been on the coin flip part for a couple hours, googling, experimenting, etc.
can’t figure out why line 9 keeps throwing the following error:
Traceback (most recent call last):
File "script.py", line 33, in <module>
heads_tails_game()
File "script.py", line 9, in heads_tails_game
bet_amount = input(f"You currently have ${money}. How much would you like to bet?")
UnboundLocalError: local variable 'money' referenced before assignment
i can’t even get past that to check the remaining code. here is my code:
import random
money = 100
def heads_tails_game():
print("Welcome to the 50/50 Game of Chance!")
bet_amount = input(f"You currently have ${money}. How much would you like to bet?")
if money < bet_amount:
print("You do not have enough money to bet that amount.")
else:
print(f"You have wagered ${bet_amount}.")
player_choice = input("Now, please choose either heads or tails. For heads, please enter the number 1. For tails, please enter the number 2.")
computer_choice = random.randint(1, 2)
if player_choice == computer_choice:
print("You are correct. Good job!")
money += bet_amount
print(f"Congratulations, you now have ${money}.")
else:
print("Oops! You were not correct.")
money -= bet_amount
print(f"You now have ${money}.")
#Call your game of chance functions here
heads_tails_game()
isn’t money already assigned outside the function, prior to the function being called?
Here it is declared in global scope,
Python only allows assignment to local variables. The error is caused because of, +=
as the first local assignment. For this purpose there is no declared variable in local scope.
Okay, it does allow assignment to globals when they are referenced objects or explicitly bound in the function…
def heads_tails_game():
global money
Now the assignment will be allowed (and money
is no longer a local variable).
as always, thanks so much!
one more question:
code works fine now, however, how do i get the function to update the money (which it now does), but then starts the function over again with the new updated money amount, so people can keep playing?
googled it, but cannot seem to find any guidance.
appreciate the time and input.
Assuming you have declared the global inside the function, money
amount is being updated each time the function is run.
Try this:
>>> def change_a():
global a
a += 1
>>> a = 0
>>> for i in range(10):
change_a()
print (a)
1
2
3
4
5
6
7
8
9
10
>>>
See how that works?
thanks, but i’m not sure i’m following. i deleted the global money variable and assigned it in the function to 100.
the code works now, it completes and totals the money. however, once it runs again, the money is reset to 100.
here is my code:
import random
def heads_tails_game():
money = 100
print("Welcome to the 50/50 Game of Chance!")
bet_amount = input(f"You currently have ${money}. How much would you like to bet? ")
if money < int(bet_amount):
print("You do not have enough money to bet that amount.")
else:
print(f"You have wagered ${bet_amount}.")
player_choice = input(
"Now, please choose either heads or tails. For heads, please enter the number 1. For tails, please enter the number 2. ")
computer_choice = random.randint(1, 2)
if int(player_choice) == computer_choice:
print("You are correct. Good job!")
money += int(bet_amount)
print(f"Congratulations, you now have ${money}.")
else:
print("Oops! You were not correct.")
money -= int(bet_amount)
print(f"You now have ${money}.")
heads_tails_game()
should i call the function again from within the function, after the last else statement? i did that, and the game runs again, but again, the money is reset to 100.
thoughts? sorry i don’t understand your previous input.
That is why it cannot be updated. It needs to maintain its global identity so the function can be run as many times as needed and the variable can be updated independent of the function.
Use,
def func():
global variable
...
variable = initial_value
run game control loop
like this?
import random
def heads_tails_game():
global money
money = 100
or like this?
import random
money = 100
def heads_tails_game():
global money
money = 100
Yes, like that. Initialize and maintain the global variable, and remove that money = 100
line from inside the function.