![]() |
I need Python help!!
Not sure if I post this here buuuut I was writing a tutorial for a text based game I was making...
Then I got stuck, I've searched all over the internet but I cannot find a way to fix this. Code:
def hello():I'd like it to be returned back to "Try saying Hello or Hi" instead of continuing, I can't seem to get it to work how I want it to :( and return doesn't work without a function but I can't get "def function" to work with if and else. And later on I'm going to make multiple doors/paths, how would I go about multiple if elif without making a huge mess? |
The core problem there is that you've got a single string in parentheses instead of a tuple full of strings for the "hi"'s. So instead of checking to see if the user's input is one of a list of options, it's checking to see if the user's input is contained as a substring within that string. And the empty string (which is what you get if you just press enter) is contained as a substring within ALL strings.
|
Ohh, alright, so when making If and Else lists, should I use {} then?
I changed it to {} and the reasponse for Hi no longer comes up, yay, how do I get it to return after else? |
No, not {}, the right fix would have been to change ('Hi, hi') to ('Hi', 'hi'). You could have also used [] but that wouldn't have fixed the actual bug (but ['Hi', 'hi'] works). {} isn't the same kind of data structure; it doesn't represent a sequence and it wouldn't do what you want.
|
Ohh!! I see it now okay! I thought I had it like ('Hi', 'hi') but that's the Hellos. I get it now, thank you.
How do I get it to return to "Say Hi or Hello." after the else message? |
Your text line is missing a ' at the end... Compare:
('Please reaspond with "Hello" or "Hi") ('Please respond with "Hello" or "Hi"') |
Sorry, I don't think I understand the question. Maybe put it in the form of a sample session demonstrating what you want to see.
|
Sorry guys, been unable to get on my laptop for awhile now. Anywhooo.
@Toho I checked my code and it's already been fixed, I'm not sure if I noticed that or if something happened in the copy or paste o_O regardless, thanks for pointing that out :) @Coda Ermm, I'm not quite sure how to do that so I guess I'll just try to explain better? Basically: "Great! Now lets continue with a reasponse try saying "Hello or Hi." #Then it prompts the use to input# |Yo "Please reaspond with "Hello" or "Hi"" #Then it brings you back to an input so you can correct it before moving on# |Hi #Then the code continues# So basically when "else" is chosen it counts as wrong and forces the player to go back, I'm actually going to need a way to send the player back for a lot of these levels, but return isn't working for me, not quite sure how to code it... |
Ah, you're looking for a loop. Try using "while True:" to make an infinite loop, and use "break" to exit the loop or "return" to exit the function when the user puts in a response that you can work with.
|
Ohh, okay, where exactly would I put "while True:" and the "break"? I can't seem to figure that out...
|
Code:
def hello(): |
AHHH!! OH MY GOD!!!! It keeps reprinting "Please reaspond with "Hello or hi." over and over again! It's hilarious but unplayable now, how do I make it stop?
One problem fixed, another caused eheh. |
Oops, my bad, the `input` call should go inside there too. Wasn't paying enough attention.
|
Perfect, that was hilarious though, I'm glad the input was forgotten, it was fun to watch.
Now I just need to keep it from re-printing "Great now lets continue with a response" after "Please respond with hello or hi" and I should be able to continue with the rest of the game for a good while :D!! Edit: WOOP! I got it all fixed up, I should be good for now :D thanks for all the help Coda! |
| All times are GMT -4. The time now is 04:15 PM. |
Powered by vBulletin®