-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")  
  • 2
    It doesn't _only_ say "syntax error". It should give you a specific error message, a line of code, and probably some additional information as well in a traceback. Please show us all of that. Copy and paste it so it's exact, please. See [ask]. – Chris Nov 24 '19 at 20:23
  • can you paste the error log? – Nergon Nov 24 '19 at 20:23
  • 1
    Your indentation is incorrect for the last `if`/`elif` – roganjosh Nov 24 '19 at 20:23
  • 1
    You may also want to look at [How to test multiple variables against a value?](https://stackoverflow.com/q/15112125/354577) when you fix your syntax error – Chris Nov 24 '19 at 20:24
  • `if randomNum == 2 or 4 or 6 or 8 or 10:` should be `if randomNum in [2,4,6,8,10]:` – splash58 Nov 24 '19 at 20:24
  • 1
    Indeed. This is broken in multiple ways. – roganjosh Nov 24 '19 at 20:25
  • 1
    You are missing a close parenthesis on the line before - `)` – Chris Nov 24 '19 at 20:25
  • If you want to test a series of numbers for even/odd as you do in this code just use ```if randomNum % 2 == 0:``` for even numbers and for odd just do ```else:``` – neutrino_logic Nov 24 '19 at 20:29
  • Some good comments so far. What have you tried to debug this? Have you done any research? – AMC Nov 24 '19 at 20:59

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
  • 505
  • 5
  • 19
  • 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,535
  • 7
  • 12
  • 34
-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
  • 27,314
  • 8
  • 98
  • 117