-2

i tried couple of things but does not work. the problem is in the 11th line(that's what python said). i am still 14 and do not have a big knowladge on programing. hope i can get some help

 def crowbar():  
   print("nice, so you picked the crowbar, and now you have to get out of the car")  
   randomNum = random.randint(1,10)  
   if randomNum == [2,4,6,8,10]:  
       print(" shoo, those 2 people are walking away. now it's the time to get out of here")  
   elif randomNum == [1,3,5,7,9]:  
        print("they are coming at your direction. you have to do something and fast")  
   choise2 = int(input("""  
                    1)hit them with the crowbar  
                    2)pretent that you are still unconscious""")) 
   if choise2 == 1: #the problem has something to do with this line 
           print("ok")  
   elif choise2 == 2:  
           print("not ok")  

3 Answers3

1

I just refactored your code and fixed several things. The program should run without any problems.

I inline commented the issues and refactorings I did:

#Added the import statement to make sure code is runnable if copy&pasted
import random

def crowbar():  
   print("nice, so you picked the crowbar, and now you have to get out of the car")  
   randomNum = random.randint(1,10)  
   #Use the "x in list" expression as it is the most readable and pythonic way
   if randomNum in [2,4,6,8,10]:  
       print("shoo, those 2 people are walking away. now it's the time to get out of here")  
   #Just use else for the second case, as this has the same behaviour without boiler plate
   else: 
       print("they are coming at your direction. you have to do something and fast")  
   choice2 = 0
   #Defensive programming: As long as neither 1 nor 2 was pressed, the input request will loop forever
   while choice2 not in [1, 2]:
       print("What do you want to do?")
       choice2 = int(input("""  
                        1)hit them with the crowbar  
                        2)pretent that you are still unconscious\n""")) 
   #I had no error here to be honest as you said. It just worked.
   if choice2 == 1:
           print("ok")  
   elif choice2 == 2:  
           print("not ok") 

if __name__ == "__main__":
    crowbar()
Faram
  • 525
  • 6
  • 21
  • I think the indentation of the statements following the `while` loop is off. Shouldn't the be inside the loop? Also, assuming that they are moved inside the loop, you're only protected against input which is a whole number other than 1 or 2. Floats, letters, etc. will crash the program. – AMC Nov 24 '19 at 21:41
-1

Alright, a few things to note:

  • To check if a value is inside of a data structure, use is in instead of ==.
  • Variable names should be descriptive and follow the lower_case_with_underscores style.
  • Your random number generation can be simplified, since currently are you need is a 50/50 chance.
  • You should probably be using a solid IDE, which would alert you to the naming issues and the incorrect membership testing.

Here is your program, I refactored it quite a bit.

import random as rand


def crowbar():
    print('You pick up the crowbar. Now you have to get out of the car.')
    random_num = rand.random()
    if random_num > 0.5:
        print('Fortunately, those 2 people are walking away. You can get away safely.')
    else:
        print('They are moving towards you, you have to react quickly.\nYou decide to...\n(1) Hit them with the '
              'crowbar.\n(2) Pretend that you are still unconscious.')
        choice = input('Your choice (1 | 2): ')
        if choice == '1':
            print('Ok.')
        elif choice == '2':
            print('Not ok.')
        else:
            print(f'Error! Incorrect input: {choice}')


crowbar()

Explanations:

  • As mentioned above, I simplified the random number generation.
  • Variable names have been improved.
  • I fixed as much of the grammar, syntax and overall writing as I could.
  • The program now verifies the input. I also got rid of the unnecessary conversion to int.
AMC
  • 2,642
  • 7
  • 13
  • 35
-4

Your indentation is incorrect and you are missing parenthesis in choice2 = ...

Also you need if randomNum in [2,4,6,8,10]: and if randomNum in [1,3,5,7,9]:

This works fine

def crowbar():  
   print("nice, so you picked the crowbar, and now you have to get out of the car")  
   randomNum = random.randint(1,10)  
   if randomNum in [2,4,6,8,10]:  # list here
       print(" shoo, those 2 people are walking away. now it's the time to get out of here")  
   elif randomNum in [1,3,5,7,9]:  # list here
        print("they are coming at your direction. you have to do something and fast")  
   choise2 = int(input("""  
                    1)hit them with the crowbar  
                    2)pretent that you are still unconcious""")) # parethesis here
   if choise2 == 1: # corrected indentation here
       print("ok")   # corrected indentation here
   elif choise2 == 2:   # corrected indentation here
       print("not ok") # corrected indentation here
seralouk
  • 30,938
  • 9
  • 118
  • 133