基本上你选择了多少行,然后它就会打印随机数到文件中。
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)
getInput()
go(user=getInput())
你看你在打电话 getInput
两次
也许你想做
user = getInput()
go(user)
# or
# go(user = getInput())
使用用户获取输入(),并以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()