Python Forum

Full Version: Question about loop
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hey everyone, I need help.
I'm creating a basic script that works like this:
The user gets a pokémon, then have to input +1 to increase its level until level 10, after the condition is true the user will be able to evolve it. For now I have this:
inicio = str(input('Input 1 to start:'))
poke = 'Pokémon'
level = 1
começou = 'You got a pokémon. Input +1 to increase its level!'
lvlup = 'Congratulations! Your pokémon grew to level: '
if inicio == '1':
 aaa = str(input(começou))
 começou = aaa
 if começou == '+1':
   print (lvlup + str(level))
   level += 1
 else:
   print('You have to input +1!')
I have no idea how can I do the while loop in this code, because I need the code to "restart" everytime the level up message is displayed.
Can someone give me some help so I can be able to finish this code please?

Thanks!
Put the while loop just before the if inicio == '1': line. Then indent everything below that one more level.

And thanks for using the python tags, so few of our first posters do.
Thank you, it really worked fine!

But now, after I input the "+1" to increase its level, I get this:

Congratulations! Your pokémon grew to level: 1
+1
Is there a way so the +1 doesn't show after the print of the level up message?

Here is the code if you want to give it a run:

inicio = str(input('Input 1 to start:'))
poke = 'Pokémon'
level = 1
começou = 'You got a pokémon. Input +1 to increase its level!'
lvlup = 'Congratulations! Your pokémon grew to level: '
while level <=10:
 if inicio == '1':
   aaa = str(input(começou))
   começou = aaa
   if começou == '+1':
     print (lvlup + str(level))
     level += 1
   else:
     print('You have to input +1!')
else:
 evolve = input('Your pokémon is ready to evolve! Input "evolve" to evolve your pokémon:')
 if evolve == 'evolve':
   print ('Congratulations! Your pokémon has evolved!')
That's because comecou is your question, but then on line nine you change it to the user's answer. So the next time you do that input, it is using comecou as the prompt, and comecou is equal to '+1'. Take out line 9, and on what is now line 10, test aaa instead of comecou. Also, you don't need to str() the input(). The input() function (in Python 3) already returns a string. In Python 2 it might not return a string, but converting back to a string wouldn't really solve the problems with that.
Ok, I got it. I can set começou as a "ready to print" text and call it whenever I need.

Thank you for you help, you can close the topic if you want sir!