0

I'm qute new into coding and I've started with python. I made a game where you get a number and you must add or subtract to this number the value of one of the two options randomly given. If you get lower than 0 or bigger than 21, the game stops with the message "You lost.". The problem is that if I imput a number that is not between the two options, the program will add this value anywais.

Can somebody help me with this problem? Also if anybody finds a way to make this game more efficient or something like that I would be so grateful.

import random

pos_ch = [-17,-16,-15,-14,-13,-12,-11,-10,-9,-8,-7,-6,-5,-4,-3,-2,-1,0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17]
pos_i = [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17]
count = random.choice(pos_i)
print("You start with: " + str(count))

while count in range(22):

    a = random.choice(pos_ch)
    b = random.choice(pos_ch)

    print("Choose one of those two options: ")
    print(a,b)
    chn_num = int(input())

    if chn_num == a or b:
        count += chn_num
    else: 
        print("Please, choose ONLY one of those TWO options:")
        chn_num2 = int(input())
        count += chn_num2


    print("Your new number is: " + str(count))

else: print('You lost')

Thank you :)

0 Answers0