0

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())
Trenton McKinney
  • 56,955
  • 33
  • 144
  • 158
Gilush
  • 81
  • 8
  • Remember - `getInput` needs to return a value on all paths. Change those internal calls to `return getInput()` or the input will be lost. – tdelaney May 22 '20 at 20:02
  • Does this answer your question? [Asking the user for input until they give a valid response](https://stackoverflow.com/questions/23294658/asking-the-user-for-input-until-they-give-a-valid-response). You're trying to validate an acceptable input and doing in incorrectly. The duplicate shows how to do this. – Trenton McKinney May 22 '20 at 20:03
  • @tdelaney can you show me an example? I got lost trying to fix that issue as well. Thank you! – Gilush May 22 '20 at 20:11
  • That post helped alot! (bookmarked). Thank you very much! – Gilush May 22 '20 at 20:18
  • I've posted a full working fix. Mine is just an FYI, the first answer is the official answer to your question. – tdelaney May 22 '20 at 20:19

5 Answers5

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

This calls getInput twice. One time on each line.

You probably want:

user = getInput()
go(user)
Thomas
  • 4,208
  • 2
  • 29
  • 31
1
getInput()
go(user=getInput())

do you see you are calling getInput twice

maybe you want to do

user = getInput()
go(user)

# or
# go(user = getInput())
ashish singh
  • 6,526
  • 2
  • 15
  • 35
0

Use user to take getInput() and go the value in user as go(user)

0

As mentioned in other answers, you've called getInput twice. There was a second issue about propegating getInput results back to the original caller. Here is the script with both fixes.

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)
tdelaney
  • 73,364
  • 6
  • 83
  • 116
0

Rewrite getInput and add 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)

full program

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()
Trenton McKinney
  • 56,955
  • 33
  • 144
  • 158