0

This program is not working right. It seems to be unable to print the cars after "You have the following cars:"

How could I get the variables to change in the def game() section?

Here is a pastebin: http://pastebin.com/pjsuWRYs

import random

car1 = ("car1")
car2 = ("car2")
car3 = ("car3")
car4 = ("car4")
car5= ("car5")
car6 = ("car6")
car7= ("car7")
car8 = ("car8")
car9 = ("car9")

def game():

        print ("You have the following cars:")

        print (car1)
        print (car2)
        print (car3)
        print (car4)
        print (car5)
        print (car6)
        print (car7)
        print (car8)
        print (car9)

        print ("You selected Grand Thief Auto")
        gta = input ("To commit GTA type GTA")
        if gta in ("gta", "Gta", "GTA", "a"):
                chance = random.randint(0,100)
                if chance <= 1:
                    print ("You stole a Bugatti Veryron")
                    car1 = ("Bugatti Veryron")

                elif chance <= 5:
                    print ("You stole a Ferrari Spider")
                    car2 = ("Ferrari Spider")

                elif chance <= 10:
                    print ("You stole a Audi Q7")
                    car3 = ("Audi Q7")

                elif chance <= 15:
                    print ("You stole a BMW X6")
                    car4 = ("BMW X6")

                elif chance <= 20:
                    print ("You stole a Jaguar X Type")
                    car5 = ("Jaguar X Type")

                elif chance <= 25:
                    print ("You stole a Ford Mondeo")
                    car6 = ("Ford Mondeo")

                elif chance <= 30:
                    print ("You stole a Audi A3")
                    car7 = ("Audi A3")

                elif chance <= 35:
                    print ("You stole a Ford Fiesta")
                    car8 = ("Ford Fiesta")

                elif chance <= 40:
                    print ("You stole a Skoda Octavia")
                    car9 = ("Skoda Octavia")

                elif chance <= 100:
                    print ("You got caught!")

                game()
game()
Yami
  • 117
  • 1
  • 2
  • 7
  • Could you be a little clearer about the problem is? – mp94 Sep 27 '14 at 22:57
  • I would say that the variables 'carX' are outside the scope of the function game(). Either you define them as globals, or define them inside the funtion or pass them as arguments to the function ([check this](http://stackoverflow.com/questions/423379/using-global-variables-in-a-function-other-than-the-one-that-created-them)) – CRM Sep 27 '14 at 23:04
  • Specifically, car1 is the problem. You have `car1=` in the function, so python thinks this must be a local variable throughout the function; but earlier in the function you print car1, which hasn't been defined yet locally. – mdurant Sep 28 '14 at 00:51

1 Answers1

0

If you want to make the "car"s accessible in the functions, you must set them as global variables.


Try putting

global car1, car2, car3, car4, car5, car6, car7, car8, car9

before the print ("You have the following cars:") and after define game():


I would also suggest using lists or dictionaries instead of defining each variable by itself.

You could have:

cars = ["car1", "car2", "etc"]

# To change a value for "car2" for example, do this: 
# List values (indexes) in Python are starting from 0
cars[1] = "Bugatti"

# To print the car name do:
print (cars[0])
print (cars[1])
# Etc.

or:

cars = { "car1" : "Bugatti", "car2" : "Ferarri", "car number" : "car name"}

# To change a value for "car2" for example, do this: 
cars["car2"] = "Bugatti"

# And to print the car names:
print (cars["car1"])
print (cars["car2"])

If you chose to use lists or dictionaries, you will have to set the cars variable global as well


To make things look nicer, I would suggest changing input ("To commit GTA type GTA") to (input ("To commit GTA type GTA: ").lower() which will add an extra space for "beauty" and make the input case-insensitive

I would also add newlines (\n) or empty print () statements to make the output prettier and easier to understand. This will just add an empty line wherever you want it to be.

Electron
  • 308
  • 4
  • 13
  • Thank you! I didn't know about this global function and it is really helpful. Thank you. – Yami Sep 28 '14 at 02:27