0

Can’t get my code to end on a certain variable

This is some homework I have to do for programming class, however I can’t figure out what I’m doing wrong. The questions instructions ask that I only use while loops, int, float, if, else/elif, and print. The question itself asks for there to be three inputs, the starting number (an integer), the end number (also an integer) and the increment to count up or down by (a float), and to use these inputs to count up or down from the start number to the end number. When I put 5 for the starting number, 1 for the end number, and 1.5 for the increment to count by, it puts out 5.0, 3.5, 2.0, and 1.5. These are the variables I am supposed to put in per the questions instructions, and I should be getting 5.0, 3.5, and 2.0. What have I done incorrectly? Sorry for the long question but any help would be appreciated https://code.sololearn.com/cTS0e1oyU281/?ref=app

27th Sep 2019, 11:35 PM
Advanced Incognizant
4 RĂ©ponses
+ 1
replace while startnum < endnum: with while startnum <= endnum: replace while startnum > endnum: with while startnum >= endnum:
28th Sep 2019, 7:04 PM
rodwynnejones
rodwynnejones - avatar
+ 2
I think this does what you need:- startnum = int(input('Enter an integer to start from:')) endnum = int(input('Enter an integer to finish at:')) increment = float(input('Enter the increment value(the step to count up or down)')) if startnum < endnum: while startnum < endnum: print(startnum) startnum+=increment elif startnum > endnum: while startnum > endnum: print(startnum) startnum-=increment else: print("Error...Upperbound and Lowerbound are the same, operation not possible.")
28th Sep 2019, 12:28 AM
rodwynnejones
rodwynnejones - avatar
0
In your while loop condition, it is not enough to check startnum < endnum, you should check startnum+increment < endnum (<= if you want the endnum to be included too). Same goes for second loop (but it's startnum - increment instead of +)
28th Sep 2019, 12:20 AM
Aymane Boukrouh
Aymane Boukrouh - avatar
0
rodwynnejones Thank you, while this does fix my origional issue, when I try startnum = 3, endum = 7, increment = 1, it outputs 3, 4, 5, and 6, but does not finish at 7. Im unsure as to how to fix this, any ideas?
28th Sep 2019, 3:50 PM
Advanced Incognizant