Python Tutorial v3.2.5 Lesson 8.2 - While Loop Application, Comments, Data Conversion

preview_player
Показать описание
0:53 Click to skip the introduction to Lesson 8.2
18:01 Click to skip directly to the Lesson 8.2 challenge program

Lesson 8.2 continues an explanation of how a while loop works and it's application in a very small guess the number program. Also discussed is the impact of the input() function always returning a string and how to adjust your program to account for that fact. A short bit of time also introduces comments, which will be used more extensively from this lesson on.

This is an introductory series of Python tutorials. This course, from start to finish, is designed to help someone who has never programmed before learn the basics of coding in Python. As this series continues, we examine more advanced Python techniques, functions, and methods.

Keep in mind this tutorial is using an older version of Python, v3.2.5. You will need to click on the "View Older Releases" button to use this specific version. Using the newer versions will not be an issue at this point, but when the lesson proceeds to basic graphics, the Pygame Module we'll use does not support v3.3+ at this time.
Рекомендации по теме
Комментарии
Автор

Steve, you have the most extensive and well done Python tutorial on the internet.

udidusumfin
Автор

Understood. Practice time for the sink :)

Noldy__
Автор

Steve is so absent minded haha
He already had to rerecord video 2 times 10 videos in for Python Tutorial
It's quite funny but I really like the videos and appreciate all the help I get!
Thanks for the tutorials, great teacher :)

nicosaloneftis
Автор

@Julio_Hernandez, Overall, that's a good start. What your missing is that in the body of the while loop, you're not adjusting the value of x, only changing it with the users input. For example, if I typed in 5, x = 5. If I then type in 3, instead of adding 3 to 5, it's simply adjusting the value to 3. You can fix this a couple of ways. You could simply change you line to:

x += int(input("What would you like to add? :"))

By doing that, you'd be adding the users input to the current value of x. You could also have the user input a number and store it in a new value, such as response, then add another line of code where x = x + response. Both would work, but the first is better programming (the second is simply another option if the first doesn't make sense).

To debug you program, you may also add a simple print(x) to the bottom of the while statement. By doing so, you can see what the value of x is each time the loop is run and catch some problems there, too. Hope that helps!

-Steve

xnavysteve
Автор

why is it saying that my top while is invalid?

import random
number = random.randint (1, 20)
guess = int(input ("Guess a number between 1-20!" )
while int(guess) < int(number):
print ("That's too low")
int(input ("Guess a number between 1-20! ")
while int(guess) > int(number):
print ("That's too high")
(int(input ("Guess a number between 1-20! ")
while int(guess) == int(number):
print ("That's correct! ")
print("Your number is now larger than 50, GAME OVER")

Really loving the series, this is without a doubt the best tutorial series I've seen. I just keep on running into weird random issues like this. If you could help me solve this problem and point out any other errors then that would be great! :)

this is a newer version of Python btw (3.6.1)

mishmeshmonster
Автор

#Initial a value to zero
x = 0
while x <= 50:
print("Your number is equal to %s" %x)
user_num= int(input("What number will you like to add? :"))
print()
x+=user_num
print("Your number is greater than 50. Final number is %s. Game over" %x )

eti-inyeneibia
Автор

Great tutorial! Can you please help me on the challenge part, I'm having trouble adding the users number to the current value. So far this is the code i have:

x = 0
number = 50

while x < number: 
    print("your number is equal to " + str(x))
    x = int(input("What would you like to add? :"))
 
print("Your number is greater than 50. Final Number was %d. Game over" % x)

    

JulioHernandez-mxbs
visit shbcf.ru