-7

so, I started to self-learn Python, but there is small problem with variables and their outcome?

choice = input()

if (random.randint(0,100) > 20 and choice == (1)):
    print("Odkopnul jsi krysu a ta sebou")
    print("prastila o zed az omdlela")

elif random.randint(0,100) < 20:
    print("I pres tvoje cukani ti")
    print("krysa ukousla kus palce!")

The problem is, when I press 1 and then enter, it does nothing :o I need it to: If random number is bigger than 20 and input was 1 to print one thing (You won, for exp.) or if random number was smaller than 20 to print other thing (You lost, for exp.)

Thank you for all advice, I appreciate that

1 Answers1

1

There are several issues with this code.

  1. input() returns a string, but you are attempting to compare it to an integer. You should first convert it to an integer by calling choice = int(input())

  2. You are calling random.randint(0,100) twice, so it returns different values each time. You should instead call it once at the top of you function, and use that value both times.

  3. You comparing you random variable to see if it is less than 20 or greater than 20, but you fail to consider the case where it is equal to 20.

  4. There is no need to place the (1) in parentheses.

Correcting these mistakes, the final code becomes

choice = int(input())
num = random.randint(0,100)

if (num >= 20 and choice == 1):
    print("Odkopnul jsi krysu a ta sebou")
    print("prastila o zed az omdlela")

elif num < 20:
    print("I pres tvoje cukani ti")
    print("krysa ukousla kus palce!")

Note that the logic of this program still doesn't entirely make sense, as there are cases where nothing will be printed. However, this corrects the more obvious errors, and I would encourage you to follow a Python tutorial learn more about the basics.

Erik Godard
  • 5,930
  • 6
  • 30
  • 33
  • 1,2 Thanks, I didn´t know that. 3 Yeah, I know about this, posted old version of code xD 4 Ok Ok, thank you very much, I will definitively continue ;) – 4kocour4 Oct 26 '16 at 14:29
  • No problem, make sure to click the check mark to accept the answers if it was useful to you. – Erik Godard Oct 26 '16 at 19:17