Trisphee

Trisphee (http://www.trisphee.com/forums/index.php)
-   Central Square (http://www.trisphee.com/forums/forumdisplay.php?f=9)
-   -   I need Python help!! (http://www.trisphee.com/forums/showthread.php?t=22787)

Satan 09-11-2017 01:15 PM

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():
    '''One of the many hellos'''
    print('Hello, this is great, but lets continue.')

def hi():
    '''One of the many... his...'''
    print('Hi, ready to stop fooling around now?')

import time
time.sleep (1)

answer_is = input('Great! Now lets continue with a reasponse\ntry saying "Hello or Hi."\n>')

if answer_is in ('hello', 'hello.', 'Hello','Hello.'):
    hello()
elif answer_is in ('hi, Hi, hi., Hi.'):
    hi()
else:
    print ('Please reaspond with "Hello" or "Hi")

When you hit Enter you get "Hi, ready to stop fooling around now?" instead of "Please reaspond with "Hello" or "Hi"" and lets say you type the wrong thing it gives you "Please reaspond with "Hello" or "Hi"" then continues the tutorial.

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?

Coda 09-11-2017 01:19 PM

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.

Satan 09-11-2017 01:27 PM

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?

Coda 09-11-2017 03:59 PM

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.

Satan 09-11-2017 04:12 PM

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?

Tohopekaliga 09-11-2017 05:04 PM

Your text line is missing a ' at the end... Compare:

('Please reaspond with "Hello" or "Hi")

('Please respond with "Hello" or "Hi"')

Coda 09-11-2017 05:05 PM

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.

Satan 09-18-2017 08:50 AM

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...

Coda 09-18-2017 10:16 AM

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.

Satan 09-18-2017 11:03 AM

Ohh, okay, where exactly would I put "while True:" and the "break"? I can't seem to figure that out...

Coda 09-18-2017 11:12 AM

Code:

def hello():
    '''One of the many hellos'''
    print('Hello, this is great, but lets continue.')

def hi():
    '''One of the many... his...'''
    print('Hi, ready to stop fooling around now?')

import time
time.sleep (1)

answer_is = input('Great! Now lets continue with a reasponse\ntry saying "Hello or Hi."\n>')

while True:
  if answer_is in ('hello', 'hello.', 'Hello','Hello.'):
      hello()
      break
  elif answer_is in ('hi, Hi, hi., Hi.'):
      hi()
      break
  else:
      print ('Please reaspond with "Hello" or "Hi")


Satan 09-18-2017 11:33 AM

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.

Coda 09-18-2017 11:34 AM

Oops, my bad, the `input` call should go inside there too. Wasn't paying enough attention.

Satan 09-18-2017 11:46 AM

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®