Home > front end >  elif checking how close to a random number a user set number is
elif checking how close to a random number a user set number is

Time:01-13

I'm trying to write a simple program to test out the basics of Python I've been learning. I decided to do a simple tongue in cheek gambling script that checks a user entered number against a 1,100 random generated number. I want to multiply the users stake by a different amount depending on how close to the random number the user guessed, but cant seem to get it to check within a range using == or <= etc. Or at least cant work it out.

i.e, user stakes 10 on number 15. if the random number is 20 it's within 5 of the users number so multiply the stake by 25. if the random number is 25 its within 10 or less of the users number so multiply by 10 etc.

Here's what I've got, would love to hear any advice on this!

if random_number == int(playernumber):
              playerstake = int(playerstake) * 100
              print("\nCongratulations, you hit the JACKPOT and won £"   str(playerstake)   "!!!")
# /- 1
       elif int(playernumber) == int(random_number)   1:
              playerstake = int(playerstake) * 50
              print("\nCongratulations, you guessed within 1 of "   str(random_number)   " and won £"   str(playerstake)   "!")
       elif int(playernumber) == int(random_number) - 1:
              playerstake = int(playerstake) * 50
              print("\nCongratulations, you guessed within 1 of "   str(random_number)   " and won £"   str(playerstake)   "!")

# /- 5
       elif int(playernumber) == int(random_number)   5:
              playerstake = int(playerstake) * 20
              print("\nCongratulations, you guessed within 5 of "   str(random_number)   " and won £"   str(playerstake)   "!")
       elif int(playernumber) == int(random_number) - 5:
              playerstake = int(playerstake) * 20
              print("\nCongratulations, you guessed within 5 of "   str(random_number)   " and won £"   str(playerstake)   "!")

etcetc I understand this currently doesn't work because I'm == random number 1 so 12 would be 13.

CodePudding user response:

Here's a simple solution. I'll let you discover how it works!

    pn = int(player_number)
    rn = random_number
    if rn == pn:
        print("You won!")
    elif rn in range(pn-1, pn 1 1):  # range goes up to the final value (less than), so you need a  1
        print("You won  - 1!")
    elif rn in range(pn-5, pn 5 1):
        print("You won  - 5!")
    else:
        print("You lost!")

The next chlallenge would be to make the 'checks', -1, -5, an array of values, but that was outside the scope of your question.

CodePudding user response:

If I'm understanding correctly, much simpler code:

dist = abs(guess - rand_num)

if dist == 0:
    multiplier = 100
elif dist <= 1:
    multiplier = 50
elif dist <= 5:
    multiplier = 20
elif dist <= 10:
    multiplier = 10
else:
    multiplier = 0
    
player_stake  = player_stake * multiplier
if dist == 0:
    print("Congratulations you've won")
else:
    print("Congratulations you guessed {0} within {1} and won ${3}".format([multiplier, rand_num, player_stake])

CodePudding user response:

Don't repeat yourself. You could replace your if clauses with a lookup table. With only 3 things, its a toss-up whether this is better, but generally, if the logic is the same within multiple conditional clauses, its good to pull that logic out into a single implementation. The lookup table lets you change the boundaries used for selection without touching the logic.

# assumes `diff`, `random_number` and `playerstake` defined at time of use
exact_text = f"Congratulations, you hit the JACKPOT and won £{playerstake}"
near_text = f"Congratulations, you guessed within {diff} of {random_number} and won £{playerstake}"

# lookup table: (diff, payout, text)
odds = [
    (0, 100, exact_text),
    (1, 50, near_text),
    (5, 20, near_text)
]

playernumber = int(playernumber)
playerstake = int(playerstake)
player_diff = abs(random_number - playernumber)

for diff, payout, text in odds:
    if player_diff <= diff:
        playerstake *= payout
        print(text)
        break
else:
    print(f"You were {player_diff} away from the random number")
  • Related