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

问题描述 投票:0回答:1

基本上你选择了多少行,然后它就会打印随机数到文件中。

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())
python function writetofile
1个回答
3
投票
getInput()
go(user=getInput())

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

你可能想。

user = getInput()
go(user)

1
投票
getInput()
go(user=getInput())

你看你在打电话 getInput 两次

也许你想做

user = getInput()
go(user)

# or
# go(user = getInput())

0
投票

使用用户获取输入(),并以go(user)的形式转入用户中的值。


0
投票

正如其他答案中提到的,你已经打电话给了 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)

0
投票

重写 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()
© www.soinside.com 2019 - 2024. All rights reserved.