Python Forum
Need help with loops!
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Need help with loops!
#1
So i have an assignment to count in increments and display them.

here is what I have :
play= True
while play:
    try:
        sValue=int(input("Enter the starting value -->  "))
    except ValueError:
        print("Please enter an integer.\n> ")
        
    try:
        eValue=int(input("Enter the ending value -->  "))
    except ValueError:
        print("please enter an integer.")
    
    try:
        iValue=int(input("Enter the increments you'd like to count in -->  "))
    except ValueError:
        print("Please enter an integer.")
        
    print("Lets beging counting, starting with", sValue, "and ending with", eValue)

    while sValue < eValue:
        sValue += iValue
        print("Value is " + str(sValue))

    again=str(input("Would you like to try again? ")).lower()
    if again == "no":
        play = False
I have 2 questions. When the input is invalid (valueerror) it moves on to the next input, i need it to re-ask the question.

2nd, when the answer is tallied, i want it to stop at the end value (eValue) instead of going over. so if the inputs are 1, 20 and 2. right now it'll output until 21, but i want it to stop at 20.

How do i do that?
Reply
#2
#1: use while loops. A function would be great here, but if you're looping you might not have gotten to those yet.

#2: you check if sValue < eValue for the while loop condition. Do the same after the increment but before printing.
Reply
#3
we've just learned about functions a bit. can you describe/show me how that helps?
Reply
#4
The first 16 lines of your code are the same 4 lines repeated 3 times. Almost any time you're copy-pasting code, you could clean it up and write a function that does it cleaner. In this case, those code could be replaced with a function, and then just calling it three times, like so:
sValue = get_int_input("Enter the starting value")
eValue = get_int_input("Enter the ending value")
iValue = get_int_input("Enter the increments you'd like to count in")
Unrelated, but those aren't great variable names. They don't give any indication as to what they mean, aside from the fact that they hold some sort of value. But holding a value isn't meaningful, since that's what all variables do :p
The program won't go faster if you use smaller variable names, so it's better for you (and anyone else who reads the code) to use meaningful names. In this case, something like starting_point, ending_point, and increment_by perhaps.
Reply
#5
I'll keep that in mind in the future.

Back to it, I've been doing my reading and was thinking this might be easier?

for x in range(sValue, eValue, iValue):
    print(x)
including the same inputs as before, above. Running it is fine but i want it to list the last digit as well. do i do this with len or something? idk i'll keep looking into it.
Reply
#6
(This is for just you asked first)If what i am thinking is wrong i am sorry but i will just ask couldn't you put continue in your except blocks ? it isn't work ?
Reply


Forum Jump:

User Panel Messages

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