简体   繁体   English

在另一个中调用 function

[英]calling a function within another one

I'm learning to code and am trying to learn to use functions more and more.我正在学习编码,并且正在尝试越来越多地学习使用函数。 I have 3 different functions here, and I haven't progressed far with the user_menu() function as I get stuck with the last piece of code in the if statement/construction.我在这里有 3 个不同的功能,并且我在user_menu() function 方面没有取得太大进展,因为我遇到了 if 语句/构造中的最后一段代码。

The times_table() function keeps repeating itself no matter what I do or what code I add.无论我做什么或添加什么代码, times_table() function 都会不断重复。 It stays on that last line of the if statement in the user_menu() function.它停留在user_menu() function 中if 语句的最后一行。 What can I do to remedy this?我能做些什么来解决这个问题?

I would also like to add the times_table2() function to the user_menu() function just under the times_table() function.我还想将times_table2() function 添加到user_menu() function 就在times_table() ZC1C425268E68385D1AB5074C17A94F1 下。

I would also like to ask for recommendations on how to combine the two times_table() functions into a single function whilst keeping the functionality.我还想就如何将两个 times_table() 函数组合成一个 function 并同时保留该功能征求建议。 I haven't come across an example online or in the book and it's quite frustrating to be very honest.我没有在网上或书中遇到过一个例子,说实话很令人沮丧。

I would appreciate some info and possibly some websites for more resources.我会很感激一些信息,可能还有一些网站可以获得更多资源。

Regards and thanks in advance.提前致以问候和感谢。

def user_menu():
    options = ["1. Would you like to learn to multiply two numbers of your choice?",
               "2. Would you like to learn to multiply a number in a table?"]
    print(*options, sep="\n")

    user_input = int(input("Please select from the list above: "))
    if user_input == 1:
        times_table(multiple="Please enter the number you wish to learn to multiply with")





def times_table(multiple):
    while True:
        multiple_text = input(multiple)
        try:
            multiple_int = int(multiple_text)
        except ValueError:
            print("Please enter a numerical value!")
            continue
            return multiple_int
def times_table2(multiple):
    while True:
        multiple_text = input(multiple)
        try:
            multiple_int = int(multiple_text)
        except ValueError:
            print("Please enter a numerical value!")
            continue
            return multiple_int

You should use return after you get correct value获得正确值后,您应该使用return

def times_table(multiple):
    while True:
        multiple_text = input(multiple)
        try:
            multiple_int = int(multiple_text)
            return multiple_int
        except ValueError:
            print("Please enter a numerical value!")

Eventually use break to exist while loop and then use return最终使用break存在while循环然后使用return

def times_table(multiple):
    while True:
        multiple_text = input(multiple)
        try:
            multiple_int = int(multiple_text)
            break # exit loop
        except ValueError:
            print("Please enter a numerical value!")

    # after `while` loop
    return multiple_int

EDIT: As I said in comment you can do it in different way.编辑:正如我在评论中所说,你可以用不同的方式来做。 If you really need to use continue then do as @Kevin suggested如果你真的需要使用continue然后按照@Kevin 的建议做

def times_table(multiple):
    while True:
        multiple_text = input(multiple)
        try:
            multiple_int = int(multiple_text)
        except ValueError:
            print("Please enter a numerical value!")
            continue

        # inside `while` but not in `except`
        return multiple_int

In second version you can also use variable True/False instead of break在第二个版本中,您还可以使用变量True/False而不是break

def times_table(multiple):
    repeat = True
    while repeat:
        multiple_text = input(multiple)
        try:
            multiple_int = int(multiple_text)
            repeat = False # exit loop
        except ValueError:
            print("Please enter a numerical value!")

    # after `while` loop
    return multiple_int

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM