Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Question about loop
#1
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!
Reply
#2
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.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
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!')
Reply
#4
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.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#5
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!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Many iterations for loop question adesimone 9 1,742 Nov-12-2022, 07:08 PM
Last Post: deanhystad
  Please check whether the code about the for loop question is correct. (SyntaxError) lilliancsk01 10 2,483 Nov-08-2022, 01:25 PM
Last Post: deanhystad
  Beginner Python Question: FIzz Buzz using while loop camoyn13 2 1,729 Sep-20-2022, 09:00 AM
Last Post: deanhystad
  Repeat question (for loop) rs74 7 6,354 Jun-17-2020, 03:17 PM
Last Post: rs74
  Question about running comparisons through loop from input value Sunioj 2 2,362 Oct-15-2019, 03:15 PM
Last Post: jefsummers
  while loop question Tripler 4 2,887 Jul-24-2018, 06:37 AM
Last Post: buran
  Loop question kraven 3 3,568 Sep-10-2017, 07:31 AM
Last Post: wavic
  Udacity while loop question liquidmetalrob 6 5,278 Jul-21-2017, 02:56 PM
Last Post: nilamo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020