Dodee
Dodee

Reputation: 31

call function to check if input is divisible by 7

A programme that check if the user input number is divisible by 7. No error until I run and enter the number, then I receive a number of errors. where is the mistake, please?

def inputdata():
    userinput=float(input("Enter number:"))
    return userinput

def processdata (userinput):

    reminder=userinput % 7
    if reminder==0:
        result=True
        return result

    else:
        result=False
        return result

def outputdata(result):
    if result==True:
        answer=(print("It is dividable by 7"))
        return answer
    if result==False:
        answer=(print("It is not dividable by 7"))
        return answer

def main():
    inputdata()
    processdata(inputdata)
    outputdata(processdata)

Upvotes: 0

Views: 1056

Answers (4)

Maxim
Maxim

Reputation: 286

I think your program is a bit to complicated in terms of the number of functions your using. Also your input wasn't converted into a integer in order to perform the calculation:

def trydivision(promt):
   """Test if what is given from the user is divisible by 7"""
   try:
      value = int(promt) % 7
      if value == 0:
         print("Yes, " + str(promt) + " is divisible by 7.")
         print("The result is " + str(value))
      else:
        print("The number " + str(promt) + " is not divisible by 7.")
        
    except ValueError:
      print("Please type an integer and not a string!")

promt = input("Enter Number: ")
trydivision(promt)

Upvotes: 0

Pygirl
Pygirl

Reputation: 13349

I have edited the main part. There were some problems with the main()

def inputdata():
    userinput=float(input("Enter number:"))
    return userinput

def processdata (userinput):

    reminder=userinput % 7
    if reminder==0:
        result=True
        return result

    else:
        result=False
        return result

def outputdata(result):
    if result==True:
        answer=(print("It is dividable by 7"))
        return answer
    if result==False:
        answer=(print("It is not dividable by 7"))
        return answer

def main(): 
    inp = inputdata() # <---------
    pr_data = processdata(inp) # <-------------
    outputdata(pr_data)

Enter number:21
It is dividable by 7

Enter number:2
It is not dividable by 7

One liner Solution:

g = lambda x:  "It is not dividable by 7" if x%7 else "It is dividable by 7"
g(int(input("Enter Number: ")))

Enter Number: 12
It is not dividable by 7

Upvotes: 1

Puddles
Puddles

Reputation: 130

I don't think you are passing parameters correctly in main(). You have:

 def main():
  inputdata()
  processdata(inputdata)
  outputdata(processdata)

but (crudely) what might work is

def main():
  outputdata(processdata(inputdata()))

Upvotes: 0

nagyl
nagyl

Reputation: 1644

Try

def divisible(x):
    return x % 7 == 0

userinput = int(input())
if divisible(userinput):
    print('Case true')
else:
    print('Case false')

Note that answer=(print("It is dividable by 7")) is not valid in python since print returns a None value

Upvotes: 0

Related Questions