This question already has answers here:
Cannot call a variable from another function
(4 answers)
Closed 2 years ago.
I have got a database program to keep data in, and I can't solve this problem:
I have got two functions. When you input A into the program
the function called addy() starts
and ask for more input into a variable
then it returns to the main screen,
then the user can Input S
which starts Show()
and then it's supposed to show what you have added into the variable
PROBLEM:
It's not getting the value from the previous definition.
CODE:
def addy():
os.system('cls')
addel = input('what is the name of the operating system?: \n')
os.system('cls')
time.sleep(1)
print(addel + ' Has been added to the database!')
time.sleep(2)
program()
def show():
print('Heres a list of the operating systems you have added:')
time.sleep(5)
program()
addel = addy()
print(addel) # this should print the value from the previous function
The are 2 reasons why
Addel is a local variable not a global one. Therefore, you can only use it in your addy function.
Say your intent was not to use it which is what it seems, you wrote
addel = addy()
the function addy has no return value so your code wont work.
to fix this write
return addel
as the last line in your addy function then it will work because now the function has a return value.
Related
I have a question: how to repeat an instruction of raw_input in python 2.7.5?
print("This is a NotePad")
main = raw_input()
this is the code(I started 3 minutes ago.)
I can't find an answer to my question on Google.
This is the code with me trying but suffering
print("This is a NotePad")
main = raw_input()
for i in range(12000):
main
The error is Process finished with exit code 0
Okay, it's not an error but it's not what I was expecting.
main = raw_input() does not make main a "macro" equivalent to raw_input(). It assigns the return value of a single call of raw_input to the name main. The closest thing to what you appear to be trying to do is to assign the value of the name raw_input itself (the function) to the name main, then call that function in the loop.
main = raw_input # Another name for the same function
for i in range(12000):
# Resolve the name lookup, then call the resulting function
main()
This question already has answers here:
Why is "None" printed after my function's output?
(7 answers)
What is the purpose of the return statement? How is it different from printing?
(15 answers)
Closed last year.
This post was edited and submitted for review last year and failed to reopen the post:
Original close reason(s) were not resolved
I have been trying to get this to output correctly. It is saying I'm not adding a line break at the end.
I was wondering, how I could add the line break? From my understanding the code is for the most part right.
I also need to have it take in another output that Zybooks generates itself, so I can't just simply put two print statements of ('*****')
def print_pattern():
print('*****')
for i in range(2):
print(print_pattern())
Expected output:
*****
*****
My output:
*****
None
*****
None
If you want your function to work, you have to return a value like this.
def print_pattern():
return '*****'
for i in range(2):
print(print_pattern())
You function isn't working properly because you are trying to print something that has no return value. If you return something and try to print like I have done in my code here, it will work.
Edit.
Since you cannot change the print_pattern() function, the correct way to do this would be like this.
def print_pattern():
print('*****')
for i in range(2):
print_pattern()
You just do a for loop where you run the function at the end of each loop. The print function my default adds a new line at the end of the print.
This question already has answers here:
How to test multiple variables for equality against a single value?
(31 answers)
Closed 1 year ago.
I would like to restart my python code from the beginning after a given "yes" input from the user, but I can't understand what I'm doing wrong here:
if input("Other questions? ") == 'yes' or 'yeah':
main()
else:
pass
my code is included within the function main().
Thanks for the help!!
You would probably do it with a while loop around the whole thing you want repeated, and as Loic RW said in the comments, just break out of the while loop:
while True:
# do whatever you want
# at the end, you ask your question:
if input("Other questions? ") in ['yes','yeah']:
# this will loop around again
pass
else:
# this will break out of the while loop
break
This question already has answers here:
Short description of the scoping rules?
(9 answers)
Closed 2 years ago.
I have a very strange problem. When I press stop, the code does not stop. see:
import tkinter as tk
x=tk.Tk()
stops=False
def stop():
stops=True
print("STOPPED")
tk.Button(x,command=stop,text="Stop").pack()
while not stops:
print(stops)
x.update()
x.update_idletasks()
If I press stop, why does still it keep on printing?
the stops variable is not edited in the while loop, so why doesn't it stop?
I have also tried adding this to the end of the while loop:
if stops:
break
But it still does not stop. Why?
I think you need to globalise the variable "stops" to modify the variable.
def stop():
global stops
stops=True
print("STOPPED")
I am bit late but with complete explanation
Yes, using global keyword will solve this issue.
Why and How ?
Explanation:
Global variables are the one that are defined and declared outside a function and we need to use them inside a function
If you create a variable with the same name inside a function, this variable will be local, and can only be used inside the function. The global variable with the same name will remain as it was, global and with the original value.
Now if you want to update this global variable via function you need to use keyword global which will update the global variable(i.e, stops)
import tkinter as tk
x=tk.Tk()
stops=False
def stop():
global stops
stops=True
print("STOPPED")
tk.Button(x, command=stop, text="Stop").pack()
while not stops:
print(stops)
x.update()
x.update_idletasks()
This question already has answers here:
Short description of the scoping rules?
(9 answers)
Closed 4 years ago.
Mac OS 10.13.16
Python 3.7
PyCharm
I was going through a tutorial for this guessing game and I came across something I thought was weird. On line 18 I call on the guess variable, which I though was a Local Variable under the for loop created above it, let's me call on it as if it were a Global. I though if a var is declared within a function or loop it makes it a local. Can someone help explain this to me.
import random
print("Hello what is your name?")
name = input()
print("Well " + name + " I am thinking of a number between 1 and 20")
secretNumber = random.randint(1,20)
for guessesTaken in range(1, 7):
print("Take a guess.")
guess = int(input())
if guess < secretNumber:
print("Sorry to low")
elif guess > secretNumber:
print("Sorry to high")
else:
break
if guess == secretNumber:
print("Great job " + name + ". You guessed my number in " + str(guessesTaken) + " moves.")
else:
print("Sorry the number I was thinking of is " + str(secretNumber))
Taken from another answer: It appears to be a design decision in the python language. Functions still have local variables, but for loops don't create local variables.
Previous proposals to make for-loop variables local to the loop have stumbled on the problem of existing code that relies on the loop variable keeping its value after exiting the loop, and it seems that this is regarded as a desirable feature.
http://mail.python.org/pipermail/python-ideas/2008-October/002109.html
Excerpt from Python's documentation:
A block is a piece of Python program text that is executed as a unit.
The following are blocks: a module, a function body, and a class
definition. Each command typed interactively is a block. A script file
(a file given as standard input to the interpreter or specified as a
command line argument to the interpreter) is a code block. A script
command (a command specified on the interpreter command line with the
‘-c’ option) is a code block. The string argument passed to the
built-in functions eval() and exec() is a code block.
And:
A scope defines the visibility of a name within a block. If a local
variable is defined in a block, its scope includes that block. If the
definition occurs in a function block, the scope extends to any blocks
contained within the defining one, unless a contained block introduces
a different binding for the name.
When a name is used in a code block, it is resolved using the nearest
enclosing scope. The set of all such scopes visible to a code block is
called the block’s environment.
Local variables are visible anywhere in the same code block. A for loop is not a code block by definition, however, and therefore the local variable defined in your for loop is still visible after the loop, within the same module.