Python Forum
code won't advance to next statement
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
code won't advance to next statement
#1
while True:                                                  # bool, True
    year = input('please enter a 4-digit year: ')
    digit = len(year)                                        # int, 4
    if digit != 4 or not year.isdigit():                     # bool, False
        print("sorry, that was bad input")
    else:
        continue
lines = open('FF_data.txt')
sum = 0.0  # float, 0.0
count = 0  # int, 0
for line in lines:
    curr_line = line.split()
    my_date = curr_line[0]  # str, list

if year == my_date[0:4]:
    sum = sum + float(curr_line[1])
    count = count + 1
lines = open('FF_data.txt')                         # list, str
sum = 0.0                                           # float, 0.0
count = 0                                           # int, 0

for line in lines:
    curr_line = line.split()                        # list, str
    my_date = curr_line[0]                          # str, list

    if year == my_date[0:4]:
        sum = sum + float(curr_line[1])             # float, float
        count = count + 1                           # int, int
        avg = sum / count

print('count',count,',sum',round(sum, 2,), 'avg',round(avg, 2))
Output:
please enter a 4-digit year: 1990 please enter a 4-digit year:
My code was working but the professor doesnt want me to nest one part inside another. When I stepped outside the loop the program stopped advancing.
Reply
#2
Don't you want to exit the loop when the input is good? Do you know what continue does?
Reply
#3
MCL169 -- Please do not double post.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Photo Python code: While loop with if statement HAMOUDA 1 583 Sep-18-2023, 11:18 AM
Last Post: deanhystad
  An unexplainable error in .format statement - but only in a larger piece of code? ToniE 4 724 Sep-05-2023, 12:50 PM
Last Post: ToniE
  List Creation and Position of Continue Statement In Regular Expression Code new_coder_231013 3 1,681 Jun-15-2022, 12:00 PM
Last Post: new_coder_231013
  Unable to understand a statement in an existing code ateestructural 1 2,238 Aug-01-2020, 09:38 PM
Last Post: deanhystad
  Advance program when certain keys are pressed? Chrislw324 2 2,348 May-19-2019, 07:13 PM
Last Post: woooee
  Trying to code backwords in if statement for different output in some scenarios skrivver99 1 2,446 Dec-03-2018, 01:32 AM
Last Post: Windspar
  Advance properties read from xml files python1234 0 2,429 Apr-25-2018, 01:42 PM
Last Post: python1234
  How to inspect the code of __exit__ method used with 'with' statement? sonicblind 3 3,386 Oct-18-2017, 01:51 PM
Last Post: sonicblind
  I need help understanding how to use and run this program! Thanks in advance! tc1chosen 6 4,816 Sep-01-2017, 01:56 PM
Last Post: tc1chosen
  loop doesn't advance-py3 text game foxtreat 2 3,582 Jun-08-2017, 06:41 AM
Last Post: foxtreat

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020