How do I call variable into another function from another function - python-2.7

I'm working on a game and am stuck on a part where a key is supposed to unlock the door of a room. Now, after some searching, I've come across that i can't call a variable that exists in one function, into another function: as i have been trying to by setting i.e.key_picked = True under a conditional in the kitchen function. And then, using a conditional in the room function, with the key_pickedin a Boolean expression.
So, how do i work around this?
def kitchen(already_there=False):
if choice02_key == "pick key" or choice02_key == "0" or choice02_key == "key":
print("---------------------------------------------------------")
print "You picked the key. It probably unlocks some door."
key_picked = True
kitchen(already_there=True)
def room01(already_there=False):
if key_pick == True:
print("---------------------------------------------------------")
print "You unlocked the room using the key."
else:
print "This room is locked."
entrance_hall(already_there=True)

You can pass the variable in a parameter. For example:
Define keyPicked in room01.
Call kitchen(already_there, keyPicked) from room01.
Make the assignment you want.
Then, you will have the value you want in keyPicked.
For example, imagine I have a function that add 10 to a number. This will be better to return the value, but it is just to show you how can you do it.
def add_ten(number):
number = number + 10
def main():
number = 5
print('Number is:', number)
add_ten(number)
print('Number is:', number)
Output:
Number is: 5
Number is: 15

Related

How do I have a user enter values between 1 and 5, where 1 will return the employee information saved in the list index [0] in python [duplicate]

I am very new to programming in Python, this is something that seems so simple, but I just don't seem to be able to get it right.
I have a list of values.
I want to prompt the user for input.
Then print out the value that's at the corresponding index number in the list.
myList [0, 1, 20, 30, 40]
choice = input()
print (......)
If the user inputs 2, I want to print the the value that is at the index 2 (20). I am unsure of what to put after print.
You can do this by accessing the list using the given input by the user.
The input given by the user I assume would be a String, so we need to use int() to change it to an integer.
print myList[int(choice)]
Additionally, you may want to first check the validity of the input supplied; it cannot be less than 0, or more than the length of the list - 1;
choice = int(choice)
if (choice < 0 || choice >= len(myList))
print('Not Valid')
else
print myList[int(choice)]

python recursion is not working as expected

I'm new to coding and trying to teach myself about recursion by building a very simple function that calls itself. However my code is behaving slightly differently to how I was expecting:
get user input number that must not be greater than 50
def getinput():
input = int(raw_input("type number under 50 >>> "))
if input < 50:
return input
else:
print input, "no, must be under 50"
getinput()
print getinput()
This results in the following behavior:
This bit as expected
C:\Python27>python recur.py
type number under 50 >>> 23
23
This bit unexpected
C:\Python27>python recur.py
type number under 50 >>> 63
63 no, must be under 50
type number under 50 >>> 23
None
My question is, why is the last line "None", and not 23? My code seems to correctly call the function again if the user inputs a number 50 or greater, but why doesn't the second call return 23 (the same as the initial output)?
Any advice much appreciated
You don't return the result of the getInput() if the number is greater than 50
def getinput():
input = int(raw_input("type number under 50 >>> "))
if input < 50:
return input
else:
print input, "no, must be under 50"
return getinput()
print getinput()
You missed a return in the else condition. The following code should work:
def getinput():
input = int(raw_input("type number under 50 >>> "))
if input < 50:
return input
else:
print input, "no, must be under 50"
return getinput()
print getinput()
In this case your function will return input
if input < 50:
return input
You are using recursion, so it is just like a stack at the end all the return values will come back to the function which was called. When the condition if input < 50 not satisfied it will return None and you are using print(getinput()).
| 23 |
| 63 | -> None
-----
That's just my understanding about recursion.
so when the value is greater than 50, do return a value instead of None to the function back.
return getinput()
Also please use different variable names instead of input.

TypeError: 'int' object is not subscriptable

So basically I have to tell the user if the staff working in a phone shop has earned a bonus. If the staff sold 4 or more phones, they get a bonus.
Problem is I keep getting this error.
if list[member_number]>=4:
TypeError: 'int' object is not subscriptable
This is my code-
How_Many_Members= int(input("Enter the number of staff members:"))
list=[]
member=1
while len(list)!= How_Many_Members:
print("how many phones did the member", member,"sold?",end=" ")
Sales=int(input(""))
list.append(Sales)
member=member+1
member_number=0
for list in range(0,How_Many_Members,1):
if list[member_number]>=4:
print("member",member_number,"has earned a bonus")
member_number=member_number+1
else:
print("member",member_number,"has not earned a bonus")
member_number=member_number+1
You have two list objects. One is an array, and the other is an object in a for statement, here:
for list in range(0,How_Many_Members,1):
You are using duplicates, and that's not good and is the thing causing your program to spit the error. It is using that list instead of the array list. And since the list in the for loop is an integer object, the error is thrown. You are basically trying to use subscript on an array but it mistakes it as the integer from the for loop because it is ambiguous. Try the following:
How_Many_Members = int(input("Enter the number of staff members:"))
list = []
member = 1
while len(list) != How_Many_Members:
print("how many phones did the member", member, "sold?", end=" ")
Sales = int(input(""))
list.append(Sales)
member += 1
member_number = 0
for _ in range(0, How_Many_Members, 1):
if list[member_number] >= 4:
print("member", member_number + 1, "has earned a bonus")
member_number += 1
else:
print("member", member_number + 1, "has not earned a bonus")
member_number += 1
Something else, you misspelled member_number in a few places and I fixed that for you. I also shorted some statements and let it print member 1 instead of member 0.
Your problem is here:
for list in range(0,How_Many_Members,1):
if list[member_number]>=4:
print("member",memeber_number,"has earned a bonus")
member_number=member_number+1
else:
print("member",memeber_number,"has not earned a bonus")
member_number=member_number+1
You are saying for list in range(0, How_Many_Members), so list is taking an integer value from 0 to How_Many_Members-1. So it's not a list anymore and you can't do list[member_number].

For Loop only looked for first element of a list while using if condition within the loop. (Python 2.7)

def has23(nums):
for i in nums:
if i == 2 or i == 3:
return True
else:
return False
print has23([4,3])
The function has to return True if list in the parameter has either 2 or 3 in it. The output resulted 'False' even though the list has 3 in it.
Why?
return ends the function and returns back to the place where the function was called. That you happened to be in a for loop inside the function isn't relevant anymore.
A function can only return once.

Python remove odd numbers and print only even

user = int(raw_input("Type 5 numbers"))
even = []
def purify(odd):
for n in odd:
even.append(n)
if n % 2 > 0:
print n
print purify(user)
Hello I am a beginner and I would like to understand what is wrong with this code.
The User chose 5 numers and I want to print the even numbers only.
Thanks for helping
There are a few problems:
You can't apply int to an overall string, just to one integer at a time.
So if your numbers are space-separated, then you should split them into a list of strings. You can either convert them immediately after input, or wait and do it within your purify function.
Also, your purify function appends every value to the list even without testing it first.
Also, your test is backwards -- you are printing only odd numbers, not even.
Finally, you should return the value of even if you want to print it outside the function, instead of printing them as you loop.
I think this edited version should work.
user_raw = raw_input("Type some space-separated numbers")
user = user_raw.split() # defaults to white space
def purify(odd):
even = []
for n in odd:
if int(n) % 2 == 0:
even.append(n)
return even
print purify(user)
raw_input returns a string and this cannot be converted to type int.
You can use this:
user = raw_input("Input 5 numbers separated by commas: ").split(",")
user = [int(i) for i in user]
def purify(x):
new_lst = []
for i in x:
if i % 2 == 0:
new_lst.append(i)
return new_lst
for search even
filter would be the simplest way to "filter" even numbers:
output = filter(lambda x:~x&1, input)
def purify(list_number):
s=[]
for number in list_number:
if number%2==0:
s+=[number]
return s