The code at line 7 works when 'Add' is typed. But the code at line 7 keeps repeating when 'View' prompt is entered. This is my password storing programe
import string
master_pass = input('Lost in Soul Sand be everything, but the KEY! \n')
while master_pass == 'Soul':
action = input('Add/View Soul?').lower()
if action == 'add':
U = input('Soulname: ')
P = input('Soul Sand: ')
with open('sandvalley.txt','a') as f:
f.write( U + '|' + P + '\n')
print('Souls saw the light')
if action == 'view':
with open('sandvalley.txt','r') as narrate:
narrate.readlines()
for row in narrate:
print(row)
if action != 'add' or 'view':
print('Soul has left the storm')
break
print ('End')
I don't know what you mean by
The code at line 7 works when 'Add' is typed. But the code at line 7 keeps repeating when 'View' prompt is entered.
since all options (add, view, and other strings) will not keep the program stay in the loop in my test and line 7 is a blank line.
But I do know if you want to use the variable action to control the program, then if action != 'add' or 'view: is certainly not the guy you looking for.
What if action != 'add' or 'view': acutally does is if (action != 'add') or ('view'):, which means if the variable action is NOT 'add' OR 'view', and the so-called Truth Value Testing is coming around. According to doc, any string whose length is not zero is considered True, hence the conditional expression will always be True and the ending process will always be done.
If you want to fix it, if action not in ['add', 'view'] will be good, but I will use
if action == 'add':
# do something
elif action == 'view':
# do something
else:
# end the program
instead, which is better IMO.
BTW, in Python 3.10+, match-case is introduced (link to doc), and this kind of cases is definitely the best time to use it.
match action:
case 'add':
# do something
case 'view':
# do something
case _:
# end the program
Give it a try.
The .readlines() function RETURNS a value so you want to save in a variabl, insted of say narrate.readlines(), say narrate = narrate.redlines().
That should work. Let me known if it work i try it already and i think it works fine:
Code
OutPut
Also you can add somthing like this:
ForCodeRepeat
this make your code repeat while your imput is != add and view.
The way you code putting action != 'add'or 'view' doesnt work because if you make the input "view" the condition action != 'add'or 'view' will be true because "view" != add. And if your input is "add" the condition will be true again because "add" != "view.
That why you want to use and instead of or becaose you want that your code stops whenyour input isn´t one of them.
Another solution could be use elif:
Elif
Because if the code won´t run if the option above is true.
Hope it helps u.
Related
I am trying to write an if, elif else clause, so that depending on the German word ending, we can see is it should go with der, die or das.
Here is my code:
word = input ("Enter word: ")
if (word.endswith('er' 'ismus')):
print ("der")
elif (word.endswith('falt' 'heit' 'keit' 'schaft' 'ung')):
print ("die")
else (word.endswith('chen' 'lein')):
print ("das")
I have also tried using suffix with square brackets but everything goes grey when I do that and so I can assume it won't work. And clearly true and false are not adequate responses for what I need. Is there anything else I can try?
Thanks in advance!
The endswith method really only checks if the word ends with one thing, but you can do something like:
def word_ends_with_one_of(word, options):
for option in options:
if word.endswith(option):
return True
return False
Then call that with:
suffix_die = ['falt', 'heit', 'keit', 'schaft', 'ung']
suffix_der = ['er', 'ismus']
suffix_das = ['chen', 'lein']
if word_ends_with_one_of(word, suffix_die):
print ("die")
elif word_ends_with_one_of(word, suffix_der):
print ("der")
elif word_ends_with_one_of(word, suffix_das):
print ("das")
As an aside, your else clause is currently problematic, it should not have a condition attached to it (unless it's a typo and you meant to have an elif instead).
Now, even though that you be a useful function to have for other purposes, you may want to consider a more application focused method since you'll be introducing a function anyway. By that, I mean something more closely suited to your specific needs, such as:
def definite_article_for(word):
# Could also use word_ends_with_one_of() in here.
if word.endswith('er'): return 'der'
if word.endswith('ismus'): return 'der'
if word.endswith('falt'): return 'die'
:
if word.endswith('lein'): return 'das'
return None
}
Then use article = definite_article_for(my_word) to get the article you want.
I have two functions, one checks if you would like to add any items to a dictionary, and then checks what the value of that item is and saves that data to a file. If you are done adding new items to the dictionary it moves to the next function that checks if you would like to change the value of any currently existing items. If everything is good to go you can command the program to exit.
My problem is creating the while loop and to make it call the first function and run it but when you have no more items to add I need it to call the next function and makes sure you don't need to change any values of currently existing items. And then when the exit command is put in it exits the while loop and the program quits. I can't figure out how to make the while loop determine the first function is over and to call the next one.
I had made the program work properly by using recurrence and no while loop. But somebody told me that was sloppy. Also I made it work when I built the functions inside the while loop, but they told me that was sloppy too. So I am trying to make the while loop after the functions are built and call for the functions inside of it. Thanks in advance and hopefully my question is clear.
# current dictionary
itemNames = {}
# checks if you want to add to your dictionary
def addToDictionary():
checkIF_newItems = raw_input("Add new item? 'YES' or 'NO' \n ").upper()
if checkIF_newItems.startswith("Y"):
newItems = raw_input("What type of item would you like to add today? \n")
newItems_Name = raw_input("What is the value of your new item? \n")
itemNames[newItems] = newItems_Name
return True
elif 'PRINT' in checkIF_newItems:
print "These are your current items. \n\n"
return True
elif checkIF_newItems.startswith("N"):
print("OKAY")
exit()
elif 'Exit' in checkIF_newItems:
exit()
# checks if you want to edit your current dictionary
def check_forChanges():
#checks user intent and if YES prints current keys
checkIf = raw_input("Change item value? 'YES' 'NO' 'EXIT' 'ADD' 'PRINT' \n").upper()
print("\n")
if checkIf.startswith("Y"):
for i in itemNames.keys():
print i
print("\n")
#finds what key to access and ask for its new value
itemChoice = raw_input("what item would you like to change the value of? \n")
return True
if itemChoice in itemNames.keys():
newName = raw_input("What is the new value? \n")
itemNames[itemChoice] = newName
print("You've changed your " + itemChoice + "'s value to " + newName + ".")
return True
print('\n')
return True
#if NO then checks to exit
elif checkIf.startswith("N"):
CLOSE = raw_input("OKAY then, would you like to exit? ").upper()
if CLOSE.startswith('Y'):
exit()
return False
elif EXIT.startswith('N'):
check_forChanges()
elif EXIT is 'print':
for i in itemNames:
print i
return True
# goes back to first function
elif 'ADD' in checkIf:
addToDictionary()
return True
#prints current values
elif 'PRINT' in checkIf:
for i in itemNames.values():
print i
return True
elif 'EXIT' in checkIf:
exit()
return False
# main routine
validIntro = False
while not validIntro:
addToDictionary()
if addToDictionary() == False:
continue
else:
exit()
check_forChanges()
if check_forChanges() == False:
break
else:
continue
But I expect to be able to run this program until the user decides to quit. Also I expect the single while loop to call both of my functions but only when necessary.
My problem is creating the while loop and to make it call the first function and run it but when you have no more items to add I need it to call the next function and makes sure you don't need to change any values of currently existing items. And then when the exit command is put in it exits the while loop and the program quits. I can't figure out how to make the while loop determine the first function is over and to call the next one.
It sounds like you want to keep adding to the dictionary until the user says "nothing more to add" and then checks the values until the user quits. That would look something like:
while addToDictionary(): pass
while check_forChanges(): pass
You'd have to modify your existing functions to False when the current action is complete and to call sys.exit() when the user asks to quit. Although I don't think you need to ask for quitting -- if they don't want to add or check, then they're done and the loops have terminated.
Do you ever want the user to add to the dictionary after check_forChanges is called? If yes, but you still want to enforce adding is done, then checking, then maybe more adding, you want a loop around the whole thing:
keep_looping = True
while keep_looping:
while addToDictionary(): pass
while check_forChanges(): pass
keep_looping = askToContinue()
I made a program in python that is supposed to accept a name as user input. It will then check if the name given is contained inside a string that is already given and if it is then the program will print out the telephone next to that name. My code is as follows:
tilefwnikos_katalogos = "Christoforos 99111111: Eirini 99556677: Costas 99222222: George 99333333: Panayiotis 99444444: Katerina 96543217"
check=str(input("Give a name: "))
for check in tilefwnikos_katalogos:
if check=="Christoforos":
arxi=check.find("Christoforos")
elif check=="Eirini":
arxi=check.find("Eirini")
elif check=="Costas":
arxi=check.find("Costas")
elif check=="George":
arxi=check.find("George")
elif check=="Panayiotis":
arxi=check.find("Panayiotis")
elif check=="Katerina":
arxi=check.find("Katerina")
s=check.find(" ",arxi)
arxi=s
y=check.find(":",arxi)
telos=y
apotelesma=tilefwnikos_katalogos[arxi+1:telos]
print(apotelesma)
But when I try to run it, I input the name and then the following message pops up:
Traceback (most recent call last):
File "C:\Users\Sotiris\Desktop\test.py", line 16, in <module> s=check.find(" ",arxi)
NameError: name 'arxi' is not defined
What am I doing wrong?
You're getting your error because arxi isn't getting defined in the first place when then name the user gave is not present on your list.You can fix that by simply adding an unconditional else case to your if/else if bundle as pointed in the comments. But the very way you tackled this problem is faulty, storing data like this in a string is a bad idea, you want to use a dictionary:
phone_catalog = {'Christoforos': 99111111, 'Eirini': 99556677, 'Costas': 99222222, 'George':99333333, 'Panayiotis':99444444, 'Katerina': 96543217}
Also check isn't a very clear variable name, maybe you should try using something better like:
user_name = str(input("Give a name: "))
And now you can do your if/elif condition but replacing it for using dictionary logic and making sure you have a final else, like such:
if user_name in phone_catalog:
print(phone_catalog[user_name])
else:
print("Unknown user")
See how the dictionary made your life much easier and your code cleaner here? Read more on Python Data Structures.
so there are a few things you have overlooked / not going as expected, the first of which is how iterating over strings in python works:
tilefwnikos_katalogos = "Christoforos 99111111: Eirini 99556677: Costas 99222222: George 99333333: Panayiotis 99444444: Katerina 96543217"
for check in tilefwnikos_katalogos:
print(check)
#print(repr(check)) #this shows it as you would write it in code ('HI' instead of just HI)
so check can never be equal to any of the things you are checking it against, and without an else statement the variable arxi is never defined. I'm assuming you meant to use the check from the user input instead of the one in the loop but I'm not sure you need the loop at all:
tilefwnikos_katalogos = "Christoforos 99111111: Eirini 99556677: Costas 99222222: George 99333333: Panayiotis 99444444: Katerina 96543217"
check=str(input("Give a name: ")) #the str() isn't really necessary, it is already a str.
if check=="Christoforos":
arxi=check.find("Christoforos")
elif check=="Eirini":
arxi=check.find("Eirini")
elif check=="Costas":
arxi=check.find("Costas")
elif check=="George":
arxi=check.find("George")
elif check=="Panayiotis":
arxi=check.find("Panayiotis")
elif check=="Katerina":
arxi=check.find("Katerina")
else: raise NotImplementedError("need a case where input is invalid")
s=check.find(" ",arxi)
arxi=s
y=check.find(":",arxi)
telos=y
apotelesma=tilefwnikos_katalogos[arxi+1:telos]
print(apotelesma)
but you could also just see if check is a substring of tilefwnikos_katalogos and deal with other conditions:
if check.isalpha() and check in tilefwnikos_katalogos:
# ^ ^ see if check is within the string
# ^ make sure the input is all letters, don't want to accept number as input
arxi=check.find(check)
else:
raise NotImplementedError("need a case where input is invalid")
although this would make an input of C and t give Cristoforos' number since it retrieves the first occurrence of the letter. An alternative approach which includes the loop (but not calling the variable check!) would be to split up the string into a list:
tilefwnikos_katalogos = "..."
check = input(...)
for entry in tilefwnikos_katalogos.split(":"):
name, number = entry.strip().split(" ")
if check == name:
apotelesma=number
break
else:
raise NotImplementedError("need a case where input is invalid")
although if you are going to parse the string anyway and you may use the data more then once it would be even better to pack the data into a dict like #BernardMeurer suggested:
data = {}
for entry in tilefwnikos_katalogos.split(":"):
name, number = entry.strip().split(" ")
data[name] = number #maybe use int(number)?
if check in data:
apotelesma = data[check]
else:
raise NotImplementedError("need a case where input is invalid")
Current assignment is building a basic text adventure. I'm having trouble with the following code. The current assignment uses only functions, and that is the way the rules of the assignment state it must be done.
def make_selections(response):
repeat = True
while repeat == True:
selection = raw_input('-> ')
for i, v in enumerate(response):
i +=1 # adds 1 to the index to make list indices correlate to a regular 1,2,3 style list
if selection == i:
print v[1]
else:
print "There's an error man, what are you doing?!?!?"
firstResponse = 'You chose option one.'
secondResponse = 'You chose option two.'
thirdResponse = 'You chose option three.'
responses = [(0, firstResponse), (1, secondResponse),( 0, thirdResponse)]
make_selections(responses)
My intention in that code is to make it so if the user selects a 1, it will return firstResponse, if the user selects 2 it will return secondResponse, etc.
I am basically just bug testing the code to make sure it produces the appropriate response, hence the "Error man..." string, but for some reason it just loops through the error message without printing the appropriate response string. Why is this?
I know that this code is enumerating the list of tuples and I can call them properly, as I can change the code to the following and get the expected output:
for i, v in enumerate(response):
i += 1 # adds 1 to the index to make list indices correlate to a regular 1,2,3 style list
print i, v
Also, two quick asides before anyone asks:
I know there is currently no way to get out of this while loop. I'm just making sure each part of my code works before I move on to the next part. Which brings me to the point of the tuples.
When I get the code working, a 0 will produce the response message and loop again, asking the user to make a different selection, whereas a 1 will produce the appropriate response, break out of the loop, and move on to the next 'room' in the story... this way I can have as many 'rooms' for as long of a story as I want, the player does not have to 'die' each time they make an incorrect selection, and each 'room' can have any arbitrary amount of options and possible responses to choose from and I don't need to keep writing separate loops for each room.
There are a few problems here.
First, there's no good reason to iterate through all the numbers just to see if one of them matches selection; you already know that will be true if 1 <= selection <= len(response), and you can then just do response[selection-1] to get the v. (If you know anything about dicts, you might be able to see an even more convenient way to write this whole thing… but if not, don't worry about it.)
But if you really want to do this exhaustive search, you shouldn't print out There is an error man after any mismatch, because then you're always going to print it at least twice. Instead, you want to only print it if all of them failed to match. You can do this by keeping track of a "matched" flag, or by using a break and an else: clause on your for loop, whichever seems simpler, but you have to do something. See break and continue Statements, and else Clauses on Loops in the tutorial for more details.
But the biggest problem is that raw_input returns a string, and there's no way a string is ever going to be equal to a number. For example, try '1' == 1 in your interactive interpreter, and it'll say False. So, what you need to do is convert the user's input into a number so you can compare it. You can do that like this:
try:
selection = int(selection)
except ValueError:
print "That's not a number!"
continue
Seems like this is a job for dictionaries in python. Not sure if your assignment allows this, but here's my code:
def make_selections(response):
selection = raw_input('-> ')
print response.get(selection, err_msg)
resp_dict = {
'1':'You chose option one.',
'2':'You chose option two.',
'3':'You chose option three.'
}
err_msg = 'Sorry, you must pick one of these choices: %s'%sorted(resp_dict.keys())
make_selections(resp_dict)
The problem is that you are comparing a string to an integer. Selection is raw input, so it comes in as a str. Convert it to an int and it will evaluate as you expect.
You can check the type of a variable by using type(var). For example, print type(selection) after you take the input will return type 'str'.
def make_selections(response):
repeat = True
while repeat == True:
selection = raw_input('-> ')
for i, v in enumerate(response):
i +=1 # adds 1 to the index to make list indices correlate to a regular 1,2,3 style list
if int(selection) == i:
print v[1]
else:
print "There's an error man, what are you doing?!?!?"
As I know, "continue" will jump back to the top of the loop. But in my case it's not jumping back, continue don't like me :(
for cases in files:
if ('python' in cases.split()):
execute_python_scripts(cases.split())
elif run_test_case(cases.split()):
continue
else:
logger("I am here")
break
In my case run_test_case() gives 1, 2, 3, 4 etc... But it always performs first(1) and jump to the else part. So I am getting the "I am here" message. It should not work like this. As I am using "continue", it should jump to the for loop.
Following is the run_test_case():
def run_test_case(job):
for x in job:
num_of_cases = num_of_cases - 1
test_type = x.split('/')
logger(log_file,"Currently "+ x +"hai!!")
if test_type[0] == 'volume':
backdoor = test_type[1].split("_")
if backdoor[0] == 'backdoor':
return get_all_nas_logs()
else:
if perform_volume_operations(x,num_of_cases) == False:
return False
else:
logger(log_file,"wrong ha!!")
Why is it always going to the else part, without jumping back to the for loop? Thanks in advance.
Here elif run_test_case(cases.split()): you are calling the run_test_case method, that will run your code to evaluate the result for the elif condition.
It only enters the block delimited by elif (in your case, continue), if the result of that method evaluates to True, otherwise it will jump to the else clause.
The problem is probably in your run_test_case code, that is never returning True, and so you'll never get the behavior that you're expecting.
It's hard to say without knowing exactly what you want to accomplish, but I'd say that you're missing a return True in the end of that code, meaning, if everything executes correctly right until the end, you want it to return True... but I'm only guessing here, you need to think about what that method is supposed to do.
In python an if or elif clause is evaluated for not only the True and False constants, but more generally for True-like and False-like values. None, for instance, is a false-like value, a non-empty string is a true-like value, etc.
Check this from the documentation on values that are considered true or false:
http://docs.python.org/library/stdtypes.html