pythonfunctionwritetofile

Why does my python input function asks for input twice?


Basically you choose how many lines, then it prints random numbers to file. The program works but I need to input the number 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())

Solution

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

    This calls getInput twice. One time on each line.

    You probably want:

    user = getInput()
    go(user)