1

Is there a way to make a variable inside an if statement global to the whole program? In the following code, I want to change my favorite color to purple, but it only does it for the if statement. For example, Apple's Siri knows your name. Let's say you for some reason want to change it, so you say "Siri change my name to Bob". Then from that point on, Siri calls you bob. I'm trying to model the same thing but with color.

color = "red"
command = input()#the variables have been defined

if command == "What is my favorite color":
    print(color, "is your favorite color")#this prints red

if command == "Change my favorite color":
color = input()
    print(color, "is your new favorite color")#this prints whatever 
                                            #color I put, purple
#now if I ask what is my favorite color again, it will still say red, but I want it to say purple from now until I change it to another color and so on
Will Pacheco
  • 11
  • 1
  • 3
  • 1
    This isn't valid Python—you can't use `=` in if-conditions, and you need to end it with a colon. Provide a [mcve] based on your *actual* code. – Arya McCarthy Jul 21 '17 at 01:31
  • Setting a var inside an IF statement doesnt affect its scope, ie. it has the same scope as if it was declared outside the IF statement. Your problem lies elsewhere – Bug Jul 21 '17 at 02:27

3 Answers3

0

I'm not sure what your issue is. Simplifying a bit:

color = 'red'

if True:
    color = 'green'
    print(color) # Prints green

print(color) # Prints green

As you can see, changing a variable within an if block also changes it within the scope of the enclosing function.

This might be a good introduction for Python scoping rules - Short Description of the Scoping Rules?

Andrew Guy
  • 9,310
  • 3
  • 28
  • 40
  • My issue is that I can ask the program "What is my favorite color" and it prints "red". Now I want to also be able to ask it to change my favorite color to purple, which means changing the variable color to whatever input I give it, so that the next time I ask what is my favorite color, it prints the new color rather than red. – Will Pacheco Jul 21 '17 at 01:44
  • Then post your full, working code. As written, it doesn't seem like you should be having issues redefining the `color` variable. Have you embedded some of these statements within function definitions? – Andrew Guy Jul 21 '17 at 01:47
  • Have you got all that code running in a loop? Please post your entire code, and tell us how you are running it, your expected output, and your actual output. – Andrew Guy Jul 21 '17 at 02:11
  • No it is not in a loop; that is the entire code. When I ask the program what my favorite color is, the reply is red, which is the first output that I want. Now I want to change it to purple, so I ask it to change it and it does for only that specific in statement. However, I want it to change the value of color for the whole program, so that next time I ask it what my favorite color is, it will print out the new color instead of red. I'm trying to copy Siri's way of changing your name. – Will Pacheco Jul 21 '17 at 02:20
  • So you're running the program multiple times?? No wonder it's not saving your variable. Maybe consider storing your favourite color in a file and reading from that. – Andrew Guy Jul 21 '17 at 03:00
  • How would I go about doing that? Thank you and I apologize I am quite new to this. – Will Pacheco Jul 21 '17 at 03:12
  • There are plenty of options. Maybe try a quick search on this site? This is a good starting point - https://stackoverflow.com/a/30139269/4497519 – Andrew Guy Jul 21 '17 at 03:16
0

I think you are not passing proper input

color = "red"
command = input()#the variables have been defined

if command == "What is my favorite color":
    print(color, "is your favorite color")#this prints red
    command = input()

if command == "Change my favorite color":
   color = input()
   print(color, "is your new favorite color")#this prints whatever 
   command = input()

if command == "What is my favorite color":
    print(color, "is your favorite color")

Execution sequence

What is my favorite color red is your favorite color

Change my favorite color

purple purple is your new favorite color

What is my favorite color purple is your favorite color

Hariom Singh
  • 3,512
  • 6
  • 28
  • 52
0

The other answers aren't quite covering your issue because they assume that you're executing the whole code only once and somehow getting both of those prints to fire (which would mean that you would have to have changed the contents of command between your two ifs).

Your issue is that you're running the program multiple times, and expecting the new value of color to somehow (magically?) propagate over to the next time you run the program.

In order to run the same bit of code multiple times, you should put it in a loop. For example:

while command != "exit":
    # your code goes here

Don't forget to indent the code that goes inside the loop.

However, if that's all you do, you're still going to have an issue (actually two, but we'll fix the command is not defined error in a sec), because the first line of your code sets color to "red", and that's going to keep setting your color variable back to "red" every time it loops.

To fix this part of the issue, you want to initialize your variables outside of the loop (what do you know, this fixes the other issue too).

Python has a special None that fits this situation well, so let's use that:

command = None # initialized but contains nothing
favcolor = input("What is your favorite color?") # let's initialize this with some user data
while command != "exit"
    command = input("Please enter a command:")
    # the rest of your code goes here

If you really need to exit the program and have the user data from last time come back, you'll have to save that data to a file or database or something, then load it back into the program next time you run it.

Hope this clears things up a bit for you.

3D1T0R
  • 1,050
  • 7
  • 17