I am a student programmer at college and I've only recently started coding in my own time. I have some basic knowledge on how to program but arrays are out of my knowledge and I am trying to learn them. I got challenged to make a fruit roll gambling machine. I need to roll 3 strings out of a possible 6 and check for certain conditions.
I have figured out how to take 3 of those out and print them but I need to check for winners etc. For 3 bells you win the jackpot, for 2 dupes + another you win a regular prize, if you roll the skull you get a deduction.
I think I can figure out how to check which ones are rolled by just using if and else if statements (not sure if it works) but it will be a lot of else if's to make sure each and every combination is considered for.
Is there a quicker way to check for duplicates? Or is just using lots of elif statements the only way to do it. I can elaborate further if required.
I've pasted the full code below, forgive me if its not well written I'm still only like beginner level, I was also told to try use functions which is why there is so many. My main problem is as I described before but any other tips for the entirety of the code will be well appreciated too. Thanks in advance.
import random
fruit = ["Cherry", "Bell", "Lemon", "Star", "Orange", "Skull"]
def fruit_machine():
credits = 10
credit = True
while credits > 0:
turn = str(input("Would you like to roll for 20p? Y/N: "))
if turn.lower() == "y":
credits = credits - 2
credits_print(credits)
elif turn.lower() == "n":
print("You have chosen not to roll.")
credit = False
else:
print("Please enter a valid option.")
return credits
return credit
def credits_print(credits):
print("You have", credits, "credits remaining")
fruit_roll()
if credits == 0:
print("--------------------------------------\nYou have no more credits remaining")
end()
def fruit_roll(credits):
roll = random.choices(fruit, k = 3)
if roll == ["Bell, Bell, Bell"]:
print("Jackpot!!!\n+5 credits")
credits = credits + 5
def end():
print(" ")
fruit_machine()
I had made the end() as I couldn't figure out how to stop it looping back to the fruit_machine function after reaching 0 credits, it would just go back and go into negatives.
There are more streamlined ways to check for duplicates and other conditions in a list rather than using a bunch of if elif statements. The logic is going to change based on the requirements and combinations of roll results. For example, is it just 3 Bells that hit the jackpot or is it 3 of any roll except skull?
An example fruit_roll function based on the requirements in your question would be something like this:
def fruit_roll(credits):
roll = random.choices(fruit, k=3)
print("Rolled: ", roll)
if roll.count("Bell") == 3:
print("Jackpot!!! +5 credits")
credits += 5
elif any(roll.count(f) == 2 for f in fruit) and "Skull" not in roll:
print("Regular prize! +2 credits")
credits += 2
if "Skull" in roll:
print("Skull rolled! -1 credit")
credits -= 1
return credits
There were some other issues with your code that I found when going through it.
The tweaked code with the roll evaluation logic is below.
import random
fruit = ["Cherry", "Bell", "Lemon", "Star", "Orange", "Skull"]
def fruit_machine():
credits = 10
while credits > 0:
turn = str(input("Would you like to roll for 20p? Y/N: "))
if turn.lower() == "y":
credits -= 2 # Deduct 20p
credits = fruit_roll(credits) # Roll fruits and update credits
credits_print(credits)
elif turn.lower() == "n":
print("You have chosen not to roll.")
break
else:
print("Please enter a valid option.")
print("--------------------------------------\nYou have no more credits remaining")
end()
def credits_print(credits):
print("You have", credits, "credits remaining")
def fruit_roll(credits):
roll = random.choices(fruit, k=3)
print("Rolled: ", roll)
if roll.count("Bell") == 3:
print("Jackpot!!! +5 credits")
credits += 5
elif any(roll.count(f) == 2 for f in fruit) and "Skull" not in roll:
print("Regular prize! +2 credits")
credits += 2
if "Skull" in roll:
print("Skull rolled! -1 credit")
credits -= 1
return credits
def end():
print("Game Over.")
fruit_machine()