I want to retrieve a variable from a function. from some reason that i cannot understand, it works in one case and in the other it runs the function again.
Will appreciate your input:
FIRST CASE: runs the fun again
def rad():
while True:
try:
raadsels=int(input("Hoeveel raadsels wil je op te lossen?\n"))
return raadsels
except ValueError:
print('De Waarde moet een getal zijn.')
else:
if raadsels ==0:
print('Je hebt gekozen om te stoppen met spelen')
break
else:
if 1 <= raadsels <= 10:
break
else:
print('De waarde moet tussen 1 en 10 liggen')
rad()
def function2(raadsels):
print(raadsels)
raadsels=rad()
function2(raadsels)
SECOND CASE: retrieve the variable only
def function1():
raadsels=int(input("Hoeveel raadsels wil je op te lossen?\n"))
return raadsels
def function2(raadsels):
print(raadsels)
raadsels = function1()
function2(raadsels)
Please read the pinned thread in order to understand how to make the code show up properly, with proper indentation, so that we can see what the code actually looks like.
To answer your question in a literal way: simply return the variable and assign it to an object that called the function:
def func1():
try:
number = int(input("Integer value: "))
except ValueError:
print("Error.")
else:
return number
get_number = func1()
if get_number:
print(get_number)
It’s not too easy to see where your code is failing, because you’ve posted said code unformatted, but I can see that you are calling rad() again at the end of the rad() function, so that may answer that question.
Thanks for the prompt reply.
I used your concept and it still doesn’t work (see below).
Would appreciate your input.
def rad():
while True:
try:
raadsels=int(input("Hoeveel raadsels wil je op te lossen?\n"))
return raadsels
except ValueError:
print('De Waarde moet een getal zijn.')
else:
if raadsels ==0:
print('Je hebt gekozen om te stoppen met spelen')
break
else:
if 1 <= raadsels <= 10:
break
else:
print('De waarde moet tussen 1 en 10 liggen')
rad()
raadsels = rad()
if raadsels:
print(raadsels)
def rad():
try:
raadsels = int(input("Hoeveel raadsels wil je op te lossen?\n"))
except ValueError:
print('De Waarde moet een getal zijn.')
else:
if raadsels == 0:
print('Je hebt gekozen om te stoppen met spelen')
break
else:
if 1 <= raadsels <= 10:
break
else:
print('De waarde moet tussen 1 en 10 liggen')
return raadsels
raadsels = rad()
if raadsels:
print(raadsels)
Although, I’m unsure what you’re trying to do with this code line: if 1 <= raadsels <= 10:
To add:
This works to some extent, but you’ll need to modify it:
def rad():
try:
raadsels = int(input("Hoeveel raadsels wil je op te lossen?\n"))
except ValueError:
print('De Waarde moet een getal zijn.')
else:
if raadsels == 0:
print('Je hebt gekozen om te stoppen met spelen')
return raadsels
raadsels = rad()
if raadsels:
print(raadsels)
When you get a return from the function, deal with that outside of the function.
God bless you…thanks again
However…still doesn’t work and I get an error message if I deal with the variable outside the function:
UnboundLocalError: local variable ‘riddles’ referenced before assignment.
I translated the code to english…might be easier:
try:
riddles = int(input("How many riddles would you like to solve?\n"))
except ValueError:
print('The value should be a number.')
else:
if riddles == 0:
print('you have chosen to stop playing')
return riddles
riddles = rad()
if riddles:
print(riddles)
if 1 <= riddles <= 10:
print('you may continue')
else:
print('range should be between 1 and 10 ')```
def rad():
riddles = False
while not riddles:
try:
riddles = int(input("How many riddles would you like to solve?\n"))
except ValueError:
print('The value should be a number.')
else:
if riddles == 0:
exit('you have chosen to stop playing')
return riddles
So, now when you run riddles = rad(), you’ll know that you have a integer number and can move the app forward in whatever way you need to, but you’ll need to deal with inputs such as -1 and the like, so you’ll still need if riddles > 0:
So, just to be clear: you want the function to return only values between 1 and 10 and keep asking for an input if the user enters anything other than a value in that range, but the user can enter a zero to exit that app, right?
If so…
def rad():
riddles = False
while not riddles:
try:
riddles = int(input("How many riddles would you like to solve?\n"))
except ValueError:
print('The value should be a number.')
else:
if riddles == 0:
exit('you have chosen to stop playing')
elif riddles not in range(11):
print("\nEnter a value between 1 and 10\n")
riddles = False
return riddles
… if not, sorry for my lack of understanding. I’ll try again once you correct me.
I kept you busy today ha
The loop didn’t exit when the input was 0 so i took what you wrote and when the input was 0 ==> print(‘you have chosen to stop playing’)==>break
NOW IT WORKS perfectly (see below).
I WISH YOU GOOD NIGHT and THANK YOU so MUCH for YOUR KIND HELP
It was a pleasure learning from you.
riddles = False
while not riddles:
try:
riddles = int(input("How many riddles would you like to solve?\n"))
except ValueError:
print('The value should be a number.')
else:
if riddles == 0:
print('you have chosen to stop playing')
break
elif riddles not in range(11):
print("\nEnter a value between 1 and 10\n")
riddles = False
return riddles
raadsels = rad()
if raadsels:
print(raadsels)```
I haven’t been involved with this thread, but I just want to say that this sort of message is a true delight to see Congrats, and GG Rob for the excellent help.