Home > Software engineering >  how to solve this fuction problem about string
how to solve this fuction problem about string

Time:05-18

    num1 = input("First natural number: ")
    if num1.isdigit():
        int(num1)
        break
while True:
    num2 = input("Second natural number: ")
    if num2.isdigit():
        int(num2)
        break
if num1 > num2:
    max = num1
    min = num2
elif num1 < num2:
    max = num2
    min = num1
else:
    print("%d & %d highest common division: %d",num1,num2,num1)
def tgcd(max,min):
    if max% min != 0:
        tgcd(max, min-1)
    else:
        print("%d & %d highest common division: %d",max,min,min)
tgcd(max,min)

when implement this code, face type error : not all arguments converted during string formatting... but i'm not use string ;;;

CodePudding user response:

Your problem stems from the improper use of formatting in your print statements.

i.e.

    print("%d & %d highest common division: %d",num1,num2,num1)

Should really be:

    print("%d & %d highest common division: %d" % (num1,num2,num1))

Ditto with the other statement. So it should finally be:

while True:
    num1 = input("First natural number: ")
    if num1.isdigit():
        num1 = int(num1) # CHANGED
        break

while True:
    num2 = input("Second natural number: ")
    if num2.isdigit():
        num2 = int(num2) # CHANGED
        break

if num1 > num2:
    max = num1
    min = num2
elif num1 < num2:
    max = num2
    min = num1
else:
    print("%d & %d highest common division: %d" % (num1,num2,num1))


def tgcd(max,min):
    if max% min != 0:
        tgcd(max, min-1)
    else:
        print("%d & %d highest common division: %d" % (max,min,min))


tgcd(max,min)

CodePudding user response:

int() does not modify in place. Modifying your code to:

while True:
    num1 = input("First natural number: ")
    if num1.isdigit():
        num1 = int(num1) # CHANGED
        break
while True:
    num2 = input("Second natural number: ")
    if num2.isdigit():
        num2 = int(num2) # CHANGED
        break
if num1 > num2:
    max = num1
    min = num2
elif num1 < num2:
    max = num2
    min = num1
else:
    print("%d & %d highest common division: %d",num1,num2,num1)
def tgcd(max,min):
    if max% min != 0:
        tgcd(max, min-1)
    else:
        print("%d & %d highest common division: %d",max,min,min)
tgcd(max,min)

resolves this problem -- the code has other issues, but this fix fixes that.

CodePudding user response:

Where the code says:

if max% min != 0:

At this point, max is a string, because num1 and num2 are both strings, because e.g. int(num1) *did not change num1. You cannot change a string into an integer "in-place" in Python. int() creates a new integer. If you want to use it, assign it back to the variable: num1 = int(num1).

Because max was a string, max % min means to use %-style string formatting. This did not work, because the max string does not have any %-code in it to be replaced.

On the other hand:

print("%d & %d highest common division: %d",num1,num2,num1)

This does not use %-style string formatting, even though there are %-codes in the first string. That is because there is no use of the % operator. Instead, num1, num2, num1 are just more arguments for the print function. We can fix this like: print("%d & %d highest common division: %d" % (num1, num2, num1)).

However, it is easier to use the modern, up-to-date approach to string formatting. It looks like this: print(f"{num1} & {num2} highest common division: {num1}"). Notice carefully the f before the " - that is necessary. Before the code runs, Python will transform that into print("{} & {} highest common division: {}".format(num1, num2, num1)), using the new .format method for strings. You can also write it that way. This formatting technique is more powerful and more elegant than the old % way. You can read more about it here and here.

  • Related