Jump to content

Beginner Python 3.0 While Loop Issue

- - - - -

  • Please log in to reply
3 replies to this topic

#1
AbstractF

AbstractF

    Newbie

  • Members
  • Pip
  • 1 posts
I've been trying to tech myself Python this summer as I heard that it's one of the more user friendly languages, yet I'm just getting with the plethora of errors or 'program disobedience'.

Here's what I'm supposed to do for an exercise in the book I'm using (no solutions...):
1-Pick any two numbers with a difference of at least 6. If the the difference exceeds six, exit the program.

2- Re-organize the numbers in ascending order and find a number between the first and second number that is divisible by 5 and print all three numbers in ascending order.


At hindsight, it seems simple. Here's my code:

import math

def main():


    numberOne= int(input("Enter first number: "))

    numberTwo= int(input("Enter second number: "))

    


    if abs(numberOne-numberTwo)!= 6:

        input("This program shall terminate")

        input()

    else:

        if numberOne < numberTwo:

            sortandmod(numberOne,numberTwo)

           

            

        else:

            b = numberOne

            a = numberTwo

            numberOne = a

            numberTwo = b

            sortandmod(numberOne,numberTwo)

            


def sortandmod(numberOne,numberTwo):

    if numberOne%5 == 0:

        n = numberOne + 1  #I STRONGLY SUSPECT THAT THE ISSUE IS TAKING PLACE HERE

        i= 0

        alpha = 1

        while alpha != 0:

            n= n + i

            alpha = n%5

            i = i+1

            lol = n

            print("lol= ",lol,"n= ",n)

    

        print("The numbers sorted are: ", numberOne,", ",n,", ",numberTwo)


    else:

        n=numberOne

        alpha = 1

        i = 0

        while alpha != 0 and n<numberTwo:

            n= numberOne + i

            alpha = n%5

            i = i+1

            lol = n

            print(lol)

        print("The numbers sorted are: ", numberOne,", ",lol,", ",numberTwo)

main()


The code works fine when I enter a first number that is not a multiple of five.

However, when I enter a number that is a multiple of five, I get a false number in between. I suspect that the while loop with my comment is the central issue.

When I type in 200 as my first number and 206 as my second number, I get "200, 210 and 206 as my output".

Thanks for the help.

#2
Carver413

Carver413

    Newbie

  • Members
  • PipPip
  • 13 posts
while I do not know python. I think you have made your program to complicated

input(a) #get a and c

input(c)

if a > c: #swap a and c if a greater then c 

  t = a  

  a = c  

  c = t  

t = c - a #find the difference

if t != 6: #end if not 6

  exit

t = c / 5 #find b 

b= t * 5

print(a) #print and end

print(b)

print(c)



#3
demo53

demo53

    Newbie

  • Members
  • PipPip
  • 10 posts
What you did looks really confusing to me. Look at what I did:


def work(numberOne,numberTwo):

    for x in range(numberOne,numberTwo+1, +1):

        if x % 5 == 0:

            return x

        else:

            continue



numberOne = int(input("Enter the first number:"))

numberTwo = int(input("Enter the second number:"))

 

if abs(numberOne-numberTwo) != 6:

    input("<The program will now exit...>")

else:

    aclist = [numberOne,numberTwo]

    aclist.sort()

print(numberOne, numberTwo, work(aclist[0],aclist[1]))


Hope this helps.

#4
pebz5

pebz5

    Newbie

  • Members
  • Pip
  • 5 posts
Hi,
This is a pretty complicated code you've put together for such a simple thing :cool:. I see that the code that I came up with was very similar to the poster before me, but I'll put it down anyways:
 

first_num = int(input("First number: "))

sec_num = int(input("Second number: "))


def div5(first_num, sec_num):

        for num in range(first_num, sec_num):

            if num % 5 == 0:

                return num


if abs(first_num - sec_num) <= 6:

    list_1 = sorted([first_num, div5(first_num, sec_num), sec_num])

    print(list_1)

else:          

    print("Goodbye!")

I'm a beginner myself, so I apologize for any errors, but it ran correctly for me.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users