0

Description: Write a function task_4_1 which takes no parameters. When called, prompt the user for an input with the string "Please enter your matriculation number:." First remove leading and trailing whitespaces. Then check if the input is an integer using the built-in functionality. If the input is not a number, then output the following: "Invalid input. Not a number…" If the input is a valid integer but is greater than (or equal to) 12000000, output the following: "Invalid input. Not a valid matriculation number…" Repeat asking the user to enter their matriculation number using the prompt above. Once the user input a valid number, your function should return it (converted to an actual integer)

My Problem is "If the input is a valid integer but is greater than (or equal to) 12000000, Output..." it's not working the way I wrote this condition, it creates an error that says "'>=' not supported between instances of 'str' and 'int'" and I dont know how to make it work the right way..

import string
def task_4_1():
    num = input("Please enter your matriculation number: ")
    if(num.isdigit()):
        return(int(num.strip()))
    elif num>=12000000:
        print("Invalid input. Not a valid matriculation number...")
        num = input("Please enter your matriculation number: ")
    else:
       print("Invalid input. Not a valid matriculation number...")
KarateKid
  • 3,138
  • 4
  • 20
  • 39
666
  • 5
  • 1
  • 3
  • @AlessandroDaRugna .. I can see that you believe its a duplicate but.. here there is no guessing and reporting back... its just validation if a value is a digit. A while-loop suffice here (excludes the triple printing). See my answer. – ZF007 Nov 02 '19 at 20:29
  • @ZF007 I think it's a duplicate because comparing `str` and `int` cannot be done. Your answer (and the answer in the duplicate) solve the root cause by calling the function `int(...)` on the input string. All the rest (while loops, guessing etc..) is irrelevant. – Alessandro Da Rugna Nov 02 '19 at 21:41
  • @AlessandroDaRugna comparing str and int is possible because their are both bits and bytes. Imagine we couldn't compare those... then we could not read each others view on this question or cars wouldn't have navigation via displays but I see your point also if you take into care comparing apples and pears by shape. Just remember..its both fruit (bits/bytes) and in the case of fruit they can be judged on vitamin/glucose levels, weight, light reflection and color. Actualy, hexidecimal... is using letter and digits for counting purpose... probs. a pearapple ;-) – ZF007 Nov 03 '19 at 00:58

2 Answers2

0

You are using the if conditions in the wrong order. Numbers above 12000000 are also a "digit", so the first condition is always satisfied and the second condition is never executed. Here is the correct code, with recursion to solve the problem of asking for input again.

import string
def task_4_1():
    num = input("Please enter your matriculation number: ")
    if(num.isdigit()):
        n = (int(num.strip()))
        if n >= 12000000:
            print("Invalid input. Not a valid matriculation number...")
            task_4_1()
        else: 
            return(n)
    else:
       print("Invalid input. Not a valid matriculation number...")
       task_4_1()
KarateKid
  • 3,138
  • 4
  • 20
  • 39
  • thats what I also thought, the the conditions are in the wrong order, but when i turned it around it also did not work, and now I see why! thank you very much! – 666 Nov 02 '19 at 18:52
  • Happy to help. You can mark the answer as correct and upvote if this is what you were looking for. – KarateKid Nov 02 '19 at 18:59
0

There is another way... the while-loop style that returns the result of your checked input (if its a digit). In all other cases it keeps looping until you provide a digit that is within the set range of digits.


import string, time

result = 0   # arbitrary value for variable chosen. You might want -1 or something else?

def task_4_1(result):

    while result == 0:

        num = input("Please enter your matriculation number: ")

        if(num.isdigit()):

            n=(int(num.strip()))

            if 0 < n < 12000000:   # digit selection parameters "0" and "12000000".
                result = n

            else:
                print ("Invalid input. Not a valid matriculation number...")
                time.sleep(2)  # pausing 2 seconds for reading comment and think about next try.


    print ("The result is %s which is a valid matriculation number" % result)

task_4_1(result)
ZF007
  • 3,708
  • 8
  • 29
  • 48