为什么我的 python 输入函数要求输入两次?

Why does my python input function asks for input twice?

基本上你选择多少行,然后它打印随机数到文件。 该程序可以运行,但我需要输入两次数字。

import random
import datetime

def getInput():

    try:
        user = int(input("How many lines? "))
        if user > 14:
            print("Max 14!")
            getInput()
        else:
            return user
    except ValueError:
        print("Numbers Only!")
        getInput()

def go(user):

    now = datetime.datetime.now()
    num = 0
    f = 'C:/Users/Gilush/Desktop/lot.txt'
    with open(f,'a') as file:
        file.write(f'{now.strftime("%d.%m.%y")}\n\n')
        while num < user:
            rand = random.sample(range(1,37), 6)
            rand.sort()
            s = random.sample(range(1,8), 1)
            file.write(f'{rand},{s}\n')
            num += 1
        file.write('======\n')
        file.close()

getInput()
go(user=getInput())
getInput()
go(user=getInput())

你看到你打了两次getInput

也许你想做

user = getInput()
go(user)

# or
# go(user = getInput())
getInput()
go(user=getInput())

这会调用 getInput 两次。每行一次。

你可能想要:

user = getInput()
go(user)

使用 user 获取 getInput() 并将 user 中的值作为 go(user)

正如其他答案中提到的,您已调用 getInput 两次。第二个问题是将 getInput 结果传回给原始调用者。这是包含两个修复程序的脚本。

import random
import datetime

def getInput():

    try:
        user = int(input("How many lines? "))
        if user > 14:
            print("Max 14!")
            return getInput()
        else:
            return user
    except ValueError:
        print("Numbers Only!")
        return getInput()

def go(user):

    now = datetime.datetime.now()
    num = 0
    # todo: removed for test
    # f = 'C:/Users/Gilush/Desktop/lot.txt'
    f = 'lot.txt'
    with open(f,'a') as file:
        file.write(f'{now.strftime("%d.%m.%y")}\n\n')
        while num < user:
            rand = random.sample(range(1,37), 6)
            rand.sort()
            s = random.sample(range(1,8), 1)
            file.write(f'{rand},{s}\n')
            num += 1
        file.write('======\n')
        file.close()

user = getInput()
go(user=user)

重写 getInput 并添加 def main()

getInput

def getInput():
    while True:
        try:
            user = int(input("How many lines? "))
        except ValueError:
            print("Numbers Only!")
            continue
        if user > 14:
            print("Max 14!")
            continue
        else:
            return user

def main()

def main():
    user_input = getInput()
    go(user_input)

完整节目

def getInput():
    while True:
        try:
            user = int(input("How many lines? "))
        except ValueError:
            print("Numbers Only!")
            continue
        if user > 14:
            print("Max 14!")
            continue
        else:
            return user


def go(user):

    now = datetime.datetime.now()
    num = 0
    f = 'C:/Users/Gilush/Desktop/lot.txt'
    with open(f,'a') as file:
        file.write(f'{now.strftime("%d.%m.%y")}\n\n')
        while num < user:
            rand = random.sample(range(1,37), 6)
            rand.sort()
            s = random.sample(range(1,8), 1)
            file.write(f'{rand},{s}\n')
            num += 1
        file.write('======\n')
        file.close()


def main():
    user_input = getInput()
    go(user_input)


main()