Questions about the code from Python 1 to N

Asked 2 years ago, Updated 2 years ago, 106 views

Hello, I'm a newbie learning Python I was studying by myself while buying a Python book, but I kept getting an error, so I'm going to ask

As the title says, add 1 to N, but I copied it exactly the same way, but there is an error.

Code is

n = int( input() )

sum = 0
for i in range(n+1)
    sum += 1

print('sum = %d'%(sum) )

What I'm going to ask is the first n = int(input()) where n means an integer, but if you don't write a number and enter it.

Traceback (most recent call last):
File "", line 1, in
n = int( input() )
ValueError: invalid literal for int() with base 10: ''

I keep getting errors. Is there any way? In the end, shouldn't we define the first line of code and go?

int python3 input

2022-09-21 18:08

1 Answers

The code int(input()) is a code that works well only if you assume that inputs consisting only of numbers that can be converted into integers. If you want to respond to general input,

in_str = input()
# Determine if in_str is a number
n = int( in_str )

This is how you'll have to put a code in the middle to check the string.


2022-09-21 18:08

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.