Playing with loops - python

I'm trying to figure out how to work with loops in Python and I need a little help. The code I wrote is just something I'm playing with. It's not an assignment or anything like that.
What I'm trying to figure out is how to loop the program so that it asks the user to input something to start the questions over. This is what I have so far:
def monkeys():
apes = "This is not a monkey!"
monkey_yes = "This is a monkey!"
is_it_a_monkey = apes + monkey_yes
monkey_question = input("Type in Gorilla, Chimp or Macaque and make sure they're capitalized: ")
for question in is_it_a_monkey:
if monkey_question == 'Gorilla' or monkey_question == "Chimp":
print(apes)
continue
else:
print(monkey_yes)
break
def main():
while True:
if again not in {"y","n"}:
print("Please enter valid input")
elif again == "n":
return "Good bye!"
elif again == "y":
return monkeys()
monkeys()
I'm trying to get main() to do most of the work since that's what my teacher wants on our assignments. Everything under main() is something I copied to see if that would work but it only returns this:
Type in Gorilla, Chimp or Macaque and make sure they're capitalized: Gorilla
This is not a monkey!
This is not a monkey!
This is not a monkey!
This is not a monkey!
It was a lot longer than just the 4 lines but this is not what I'm looking for.

I see a couple of problems here...
First of all, you're concatenating two strings...
apes = "This is not a monkey!"
monkey_yes = "This is a monkey!"
is_it_a_monkey = apes + monkey_yes
When you go into your for-loop, the interpreter is looking at each character of that concatenated string. This is why you output 38 lines of "This is not a monkey!" For what you're trying to do, you don't need a for-loop. Try this instead:
def monkeys():
apes = "This is not a monkey!"
monkey_yes = "This is a monkey!"
monkey_question = input("Type in Gorilla, Chimp or Macaque and make sure they're capitalized: ")
if monkey_question == 'Gorilla' or monkey_question == "Chimp":
print(apes)
else:
print(monkey_yes)
The next issue I see is that you don't call the main function at all. Instead of calling moneys() at the bottom of your code, call main().
Next issue is using "while True:". If you're going to use a boolean as a while condition, make sure you put logic in your code to change that condition. Changing it to "False" should be what exits your main, not a return statement. Your main() would be better off starting like this:
def main():
keep_going = True
while keep_going:
monkeys()
Notice you should call your monkeys() function first, otherwise nobody will know what to input when the program starts. You also need code asking if they want to continue running the program. Right after your monkey() call, do something like this:
monkeys()
again = input("Would you like to try again? (y/n) ")
The next issue is your use of return statements. Instead of doing this:
elif again == "n":
return "Good bye!"
do this...
elif again == "n":
print("Good bye!")
keep_going = False
Lastly, you have "if again not in {"y","n"}:". You have to assign a value to "again" or you'll get more errors. If you use the example above, it should meet your needs.
Keep plugging at it and don't lose hope. You're getting close to understanding it.

First of all, you call monkeys() but you should call main() instead since that's where the loop is. Second, return causes a function to halt execution and to continue where you called it. In order to continue looping, remove the returns in main().
To get a better understanding of how your code works read this article about debugging. It shows some tips that allow you to see step-by-step what your code is doing.

You should use this code instead, which (as many have pointed out including #Code-Apprentice above) calls the function main() as you intend. #Code-Apprentice 's answer also includes a good explanation of why this works. This code is almost exactly the same as yours above; only the last line is different:
def monkeys():
apes = "This is not a monkey!"
monkey_yes = "This is a monkey!"
is_it_a_monkey = apes + monkey_yes
monkey_question = input("Type in Gorilla, Chimp or Macaque and make sure they're capitalized: ")
for question in is_it_a_monkey:
if monkey_question == 'Gorilla' or monkey_question == "Chimp":
print(apes)
continue
else:
print(monkey_yes)
break
def main():
while True:
if again not in {"y","n"}:
print("Please enter valid input")
elif again == "n":
return "Good bye!"
elif again == "y":
return monkeys()
main()
The reason I am submitting this answer is to point out that, despite calling main(), you are going to encounter another problem right away. You refer to a variable again in main(), but you never assign a value to again in the first place (if you run this code, you can expect to see an error of the form, "NameError: global name 'again' is not defined."). Addressing this next error is beyond the scope of your question; there are lots of ways that you can re-write this code to accommodate again, if you choose.

Related

Comparing raw input to an specific element in an array using Python

I have a little piece of code in Python where I'm trying to compare a user input to a specific element in an array. Here is the code:
movies = ["movie 1", "movie2", "movie3"];
answer = raw_input("What is your guess: ")
if answer == movies[1]
then print ("yes that is correct")
else:
print ("no that is incorrect")
I know the indentation above looks wrong becasue I typed it out in the text box and I'm new to this site as well as python.
I also know that I probably need to use some sort of conditional loop, maybe a while loop, but I'm having trouble finding where I can compare user input string value to a string value in my array. Any ideas how I might accomplish this?
Have fun with Python! I guess you are trying to make a loop which keeps receiving inputs from user to compare with the desired input until user types the correct input. If so, one way, it can be implemented as following (but think of adding a break condition, like input == "Bored" , to avoid infinite loop and hard stopping your code):
movies = ["movie 1", "movie2", "movie3"]
correctAnswer = movies[1]
is_notCorrect = True
while(is_notCorrect):
answer = raw_input("What is your guess: ")
if answer == correctAnswer:
print("Yes, that is correct")
is_notCorrect = False
else:
print("No, that is incorrect")
In the code above, when is_notCorrect turns into False. At next condition checking, it will break condition, and done with the loop.
Your code has some issues
movies = ["movie 1", "movie2", "movie3"]; # No need the semi-colon in Python
answer = raw_input("What is your guess: ")
# Need a colon here after if condition, new line, and indent.
#If you don't like the colon, you need to write a different way with one line of code Eg: <Do A> if <Condition happens> else <Do B>
if answer == movies[1]
then print ("yes that is correct") # No then in if-else statement in Python
else:
print ("no that is incorrect")

Hey beginner still learning python

def firstdecision():
decision1 = ""
while decision1 != "Y" and decision1 != "N":
decision1 = input(" Do you belive the old man's story ? ( Y / N ) ")
if decision1 == "Y" :
print ("You take a bite of from the apple")
elif decision1 == "N" :
print ("You unsheath your hidden dagger and hold it to the old man's throat")
elif decision1 != "Y" and "N" :
return decision1
firstdecision()
So trying to make a text based game as a project to help me understand functions, loops better and what better way to actually learn than to get involved. Anyways kind of stuck here after the user inputs Y or N how would I code it where I can make a new def function() where depending on their answer (Y / N ) a different outcome happens?
What you should do is define the functions you want to call for each part of the dialogue and have a main function to call each one on answer, like:
def main():
if (decision1()):
decision2()
else:
decision3()
Where decision1() will return true or false depending on the user's answer.
By the way that is not a very smart way to make such game as you will soon run into a lot of function and if/elses, good exercise for beginners tough.
You don't define a new function at that point. Instead, you define both functions above this point in the code. Based on the user's response, you call one function or the other.

How can I do a break now?

I began scripting today and don't know pretty much.
But how do I get it to end?
passwort = "Admin"
Logout = "Logout"
versuch = 0
while versuch != passwort:
versuch = str(input("Passwort: "))
while versuch !=Logout:
print ("Menu:Logout")
vesuch = str(input(">"))
if str(input) == "Logout":
print ("Do you really want to Logout?")
print("Y / N")
Main issue here is in second while loop:
while versuch !=Logout:
print ("Menu:Logout")
vesuch = str(input(">")) #Precisely here
You are not checking with the same variable in the while loop condition and in the assignment, may be mistyping mistake (in While loop condition it is: versuch, and in your assignment it is vesuch). so you should change it in your assignment to versuch
Another mistake here, is here:
if str(input) == "Logout":
you are not calling the input function here but just getting its reference, you need to add parenthesis to call the function: input()
Last thing, use raw_input() instead of input() to avoid explicitly adding the double quotations in the user input otherwise it will not match, so with your code, if I enter : Admin , it will not match, but if I enter "Admin" it will work,
You don't want to bother the user to each time add the double quotations to their input, right?
Also don't need the str() function if you use raw_input() because it already returns a string
If you're using Py3+ (which you are, judging by the print()) you don't have to explicitly convert the input to a string!
versuch = input("Passwort: ")
Doing the above is enough!
Coming to the if, you probably want to do something along the lines of:
while something:
fooo foooo
fooo foooo
if versuch == "Logout":
fooo
if input() == "Y":
break
Don't forget to indent it enough to put the if statement inside the while block!
PS: Welcome to Python!

In this very basic code i can't figure out what's the sytax error here in line 6 is (python)

myName = input("Hey there, what's your name?")
print("Hello",myName,"!")
print("Here's a game called ''Guess my number'', in this game you will have to guess my number in 5 tips, I will think of a number between 1 and 20.")
ready = input("Are you readyyyy!?")
if ready = "yes" or "yeah" or "totally" or "hell yeah" or "yupp" or "yepp" or "uhumm" or "sure": <-- here's the problem it says, at "sure"'s 1st "-sign
print("Let's go!")
loop = "y"
else:
print("I'm sorry to hear that.")
loop "n"
Could please anyone help, beginner here. I tried to delete and add new word, I restared the program and the computer because there's something clearly wrong. If I delete a word like "sure" the pointer will still point to the same exact place but there's nothing there...
You're using a single = sign in your if statement. That's not allowed. If you want to check for equality, you'll need to use ==. The = operator is only for assignment statements.
While changing = to == will fix the syntax error, your code still won't work exactly right. That's because == will not be distributed over all the or options you show. The expression a == b or c gets interpreted as (a == b) or c, and if c is "truthy" (as any non-empty string will be), the expression will be considered true.
Instead, you probably want to use something like if ready in {"yes", "yeah", "totally"}. This creates a constant set object and tests if the value of the ready variable is in the set (which is a fast check).
You are using a = instead of a == in your if statement. However, I would recommend doing if ready.lower() in {"yes", "yeah", "totally", "hell yeah", "yupp", "yepp"} to account for them using all uppercase.
Also, you seem to be missing your actual loop statements. I noticed you had variables named loop that are 'y' and 'n' but don't actually use them. You should also do something like this:
myName = input("Hey there, what's your name?")
print("Hello",myName,"!")
print("Here's a game called ''Guess my number'', in this game you will have to guess my number in 5 tips, I will think of a number between 1 and 20.")
loop = True
while loop:
ready = input("Are you readyyyy!?")
if ready.lower() in {"yes", "yeah", "totally", "hell yeah", "yupp", "yepp", "uhumm", "sure"}:
print("Let's go!")
loop = False
#To break out of the while loop that will keep asking them when they are ready
else:
print("I'm sorry to hear that.")

(Python) For loop syntax - execute for only one item?

Pretty new to python/programming in general, this is my biggest project yet.
I am writing a program that will do SUVAT equations for you. (SUVAT equations are used to find the displacement, start/end velocity, acceleration and time travelled by an object with constant velocity, you may call them something different.)
I made this list:
variables = ["Displacement", "Start Velocity", "End Velocity", "Acceleration", "Time"]
which is used in the following while/for loop:
a = 0
while a==0:
for variable in variables:
# choice1 is what the user is looking to calculate
choice1 = raw_input("Welcome to Mattin's SVUVAT Simulator! Choose the value you are trying to find. You can pick from " + str(variables))
# will execute the following code when the for loop reaches an item that matches the raw_input
if choice1 == variable:
print "You chave chosen", choice1
variables.remove(variable) #Removes the chosen variable from the list, so the new list can be used later on
a = 1 # Ends the for loop by making the while loop false
# This part is so that the error message will not show when the raw_input does not match with the 4 items in the list the user has not chosen
else:
if choice1 == "Displacement":
pass
elif choice1 == "Start Velocity":
pass
elif choice1 == "End Velocity":
pass
elif choice1 == "Acceleration":
pass
# This error message will show if the input did not match any item in the list
else:
print "Sorry, I didn't understand that, try again. Make sure your spelling is correct (Case Sensitive), and that you did not inlcude the quotation marks."
Hopefully the comments I have written in the code should explain my intentions, if not, feel free to ask anything.
The problem is that when I run the code, and input choice1, the for loop activates the last line of code:
else:
print "Sorry, I didn't understand that, try again. Make sure your spelling is correct (Case Sensitive), and that you did not inlcude the quotation marks."
and then prompts me to enter the input again, and will do this as many times as it needs to get to the item on the list that I am typing.
However, I specifically coded that if what I input does not match the item on the list the for loop is currently checking, but does match one of the other items on the list, then it should pass and loop round to checking the next item.
I am probably doing something stupid, but I don't see it, so please help me figure out what I have to do to get my desired result? I assumed it was the syntax I had wrong so that is why that is the title.
Thanks for any help, I appreciate it.
Besides the problem with the indentation in your pasted code, I would rewrite it as such:
while True:
choice = raw_input('...')
if choice in variables:
print "You chave chosen", choice
# Remove the chosen member from the list
variables = [v for v in variables if v != choice]
# Break out of loop
break
# Print error messages etc.
Also remember that string comparisons are case sensitive. I.e 'Displacement' != 'displacement'.

Categories

Resources