简体   繁体   中英

Can someone explain why var = input() in python doesn't work?

You want to know your grade in Computer Science, so write a program that continuously takes grades between 0 and 100 to standard input until you input "stop", at which point it should print your average to standard output.

NOTE: When reading the input, do not display a prompt for the user. Use the input() function with no prompt string. Here is an example:

grade = input()

grade = input()
count = 0
sum = 0

while grade != "stop":
    grade = input()
    sum += int(grade)
    count += 1
    print(sum / count)

Please dont solve it for me, but if you can point out why setting grade as "input()" doesnt work

You input a line as the first operation and then correctly enter the loop only if it isn't "stop".

However, that should then be the value you use for summing rather than immediately asking the user for another value. In your current code, if the user enters "stop", there is no check before attempting to treat it as a number.

So, if you don't want a solution, I'd suggest you stop reading at this point :-)


Couldn't resist, could you? :-)


The solution is to simply move the second input call to the bottom of the loop, not the top. This will do the check on the last thing entered, be that before the loop starts or after the value has been checked and accumulated.

In addition, your print statement is inside the loop where it will print after every entry. It would be better

There's other things you may want to consider as well, such as:

  • moving your print outside the loop since currently you print a line for every input value. You'll also have to catch the possibility that you may divide by zero (if the first thing entered was "stop").
  • handling non-numeric input that isn't "stop";
  • handling numeric input outside the 0..100 range.

Don't use this since you're trying to educate yourself (kudos on you "please don't solve it for me" comment by the way) and educators will check sites like SO for plagiarism, but a more robust solution could start with something like:

# Init stuff needed for calculating mean.

(count, total) = (0, 0)

#Get first grade, start processing unless stop.

grade = input()
while grade != "stop":
    # Convert to number and accumulate, invalid number (or
    # out of range one) will cause exception and not accumulate.

    try:
        current = int(grade)
        if current < 0 or current > 100:
            throw("range")

        # Only reaches here if number valid.

        total += int(grade)
        count += 1
    except:
        print(f'Invalid input: {grade}, try again')

    # Get next grade and check again at loop start.

    grade = input()

# If we entered at least one valid number, report mean.

if count > 0:
    print(total / count)
  1. the first input does not work, covered by the second input;
  2. when input is "stop" , int("stop") is wrong;
  3. When reading the input, do not display a prompt for the user. you should print the ans after the while loop

you can use endless loop and break to solve this problem.

...

while True:
    grade = input()
    if grade == 'stop':
        break
    ...

print(ans)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM