Я очень новичок в Python и имею проблему с программой, которую я выполняю для класса. main() и create_file, но когда он добирается до read_file, интерпретатор просто сидит там. Программа работает, но ничего не происходит.Функция Python не работает, интерпретатор не дает никаких ошибок
Ответ, вероятно, очень простой, но я просто не вижу его. Заранее благодарю за любую помощь. (. Python и IDLE V 3.5.2)
Я использую IDLE
Вот код:
import random
FILENAME = "randomNumbers.txt"
def create_file(userNum):
#Create and open the randomNumbers.txt file
randomOutput = open(FILENAME, 'w')
#Generate random numbers and write them to the file
for num in range(userNum):
num = random.randint(1, 500)
randomOutput.write(str(num) + '\n')
#Confirm data written
print("Data written to file.")
#Close the file
randomOutput.close()
def read_file():
#Open the random number file
randomInput = open(FILENAME, 'r')
#Declare variables
entry = randomInput.readline()
count = 0
total = 0
#Check for eof, read in data, and add it
while entry != '':
num = int(entry)
total += num
count += 1
#Print the total and the number of random numbers
print("The total is:", total)
print("The number of random numbers generated and added is:", count)
#Close the file
randomInput.close()
def main():
#Get user data
numGenerate = int(input("Enter the number of random numbers to generate: "))
#Call create_file function
create_file(numGenerate)
#Call read_file function
read_file()
main()
'во время записи =«»:' Вы никогда не измените 'entry' внутри цикла, так что он будет держать зацикливания , – Barmar
Вы уверены, что это действительно работает? Вы определили некоторые функции, но вы когда-нибудь называли их? – sytech
@Gator_Python Посмотрите на последнюю строку: 'main()' – Barmar