gpt4 book ai didi

python - 在Python中使用get Total函数

转载 作者:行者123 更新时间:2023-12-01 05:23:40 25 4
gpt4 key购买 nike

任何人都可以帮我完成这段代码吗?我需要计算文件中包含的数字总数,并使用 get_total 函数将答案打印到屏幕上。我的其余代码运行良好,我只是不知道如何将数字加在一起并显示答案。我正在使用 python 3.3.2

def main():

filename = input("Welcome, please enter file name:\t")
menu()
choice= int(input("Enter menu choice:\t"))

while choice != 5:
#get file choice from user
if choice == 1:
#create file
create(filename)
elif choice == 2:
#read file
read(filename)
elif choice == 3:
#append file
append(filename)
elif choice == 4:
#get total
get_total(filename)

choice = int(input("Enter menu choice:\t"))

print("\nApplication Complete")

def menu():
#user chooses a number from menu
print("Choose a number to continue:\t\n\
Select 1 to create a file\n\
Select 2 to read a file\n\
Select 3 to append to a file\n\
Select 4 to calculate the total of a file\n\
Select 5 to exit programme")

def create(filename):

#create file name
outfile = open(filename,"w")
again = "y"

while again == "y":

try:
#user inputs integer
num = int(input("Input number:\t"))
outfile.write(str(num)+"\n")
#asks user whether they want to continue or not
again = input("Enter y to continue:\t")

#if an error occurs
except ValueError:
print("An error occured,please enter an integer:\t")

except:
print("An undetermined error occurred")
#close file
outfile.close()

def read(filename):

print("\nReading File")

try:
#read integers entered onto file
infile = open(filename,"r")

for line in infile:

number = int(line)
print(number)

except IOError:
print("An error occured trying to read")
print("the file", filename)

except:
print("An undefined error occurred")

def append(filename):

print("\nAppending to file")

#user enters integers again
outfile = open(filename, "a")
again = "y"

while again == "y":

try:
num = int(input("Input number to append to file:\t"))
outfile.write(str(num)+"\n")
again = input ("Enter y to continue:\t")

except ValueError:
print("an error occured please an integer")
except:
print("an undefined error occured")


outfile.close()

def get_total(filename):

print("\nGetting total numbers contained in file")

try:
#user inputs integer
num = int(input("Input number:\t"))
outfile.write(str(num)+"\n")
#asks user whether they want to continue or not
again = input("Enter y to continue:\t")

except IOError:
print("An error occured trying to read")
print("the file", filename)

except:
print("An undefined error occurred")

#call main
main()

感谢您的帮助

最佳答案

import re
def get_total(filename):

print("\nGetting total numbers contained in file")
num = int(input("Input number:\t"))
f = open(fileName)
found = 0
for line in f:
for matchNum in re.findall(r'[0-9]+',line):
if matchNum == str(num):
found = found + 1
print("\nNo. of occurrences of num in file :: ",found

希望这能解决您的问题。

关于python - 在Python中使用get Total函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21812668/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com