简体   繁体   English

Python 函数回调和 if 问题

[英]Python function callback and if issue

I just ended to write my Python code for an assignment but I'm a bit stuck on a last point, could you help me to figure out why ?我刚刚结束为作业编写 Python 代码,但我在最后一点上有点卡住了,你能帮我找出原因吗?

Here is my code :这是我的代码:

import random

def get_rand_int(arg1, arg2):

    rand = random.randint(arg1, arg2)
    if float(rand) == float(arg1):
        return None
    elif float(rand) == float(arg2):
        return None
    else:
        return rand 

def print_random():
    try :
        prompt = int(input("Please enter an integer greater than 0:"))
        assert(prompt > 0)

    except:
        print("Invalid input: You must input an integer greater than 0 for your input.")

    rand = get_rand_int(0, prompt)

    if rand > 0: 
        print( "Your random number is ",rand)
    elif rand is None:
        print( " Please Launch the program again")

The second function call the first one to generate a random number according to the user prompt.第二个函数调用第一个函数根据用户提示生成一个随机数。 Everything work except in the case that the generate random number is the same than one of the argument, my program should print " Please launch the program again".除了生成随机数与参数之一相同的情况外,一切正常,我的程序应该打印“请再次启动程序”。 However its not working and nothing happen in this situation ( nothing is printed ) Do you have any idea to how make properly work this last if loop?然而,它不工作,在这种情况下什么也没有发生(没有打印任何东西)你知道如何使最后一个 if 循环正常工作吗?

Thanks谢谢

Try this solution:试试这个解决方案:

import random
def get_rand_int(arg1, arg2):

    rand = random.randint(arg1, arg2)
    #print("Test", rand)
    if rand == arg1:
        return None
    elif rand == arg2:
        return None
    else:
        return rand 

def print_random():
    try :
        prompt = int(input("Please enter an integer greater than 0:"))
        assert(prompt > 0)

    except:
        print("Invalid input: You must input an integer greater than 0 for your input.")

    rand = get_rand_int(0, prompt)

    if rand != None: 
        print( "Your random number is ",rand)
    else:
        print( " Please Launch the program again")

print_random()

More Optimal Solution:更优的解决方案:

import random
def get_rand_int(arg1, arg2):
    rand = random.randint(arg1, arg2)
    if rand != arg1 or rand != arg2:
        return rand
    return None

def print_random():
    try :
        prompt = int(input("Please enter an integer greater than 0:"))
        assert(prompt > 0)

    except:
        print("Invalid input: You must input an integer greater than 0 for your input.")

    rand = get_rand_int(0, prompt)

    if rand != None: 
        print( "Your random number is ",rand)
    else:
        print( " Please Launch the program again")

print_random()

Output:输出:

Please enter an integer greater than 0:7                                                                                                                                                                                            
Your random number is  2 

Another Output:另一个输出:

Please enter an integer greater than 0:0                                                                              
Invalid input: You must input an integer greater than 0 for your input.                                               
Please Launch the program again

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

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