• 회원가입
  • 로그인
  • 구글아이디로 로그인

[howto] Python - 사용자 입력값 더하기 ★ (Add Two Numbers)

9916  

값 입력받아 연산

[더하기

 

x = 3

y = 4

print(x + y)

 

결과값: 7


[사용자로부터 특정 개수의 값을 입력받아 더하기]

 

x = input("숫자 입력: ")

y = input("숫자 입력: ")

sum = int(x) + int(y)

print("헙계: ", sum)

 


[사용자로부터 랜덤 개수의 값을 입력받아 더하기]

 

x = input('더할 여러 숫자를 공백으로 구분해 입력:').split()

def sum_all(a):

    sum = 0

    for i in range(len(a)):

        sum = sum + int(a[i])

    return sum

print(sum_all(x))

 

PS. 들여쓰기 주의

 

PS1. 

input() 함수로 입력 받으면, 기본 str (문자열) 타입임. 

따라서, 숫자 입력 받을 때, 꼭 데이터타입 변경 필요함.

데이터타입 변경 후, 잘 바뀌었는지 꼭 테이터타입 확인.

 

PS2.

한번에 여러 숫자 입력받아, 각각의 변수에 할당하기 ★

(예) a,b,c = input('3개 숫자 입력: ').split()

※ split() 메서드로 쪼개면 기본적으로 List 자료형이 됨.

len() 함수, range() 함수, for 반복문 이용해 더하기



분류 제목
module Python - math.erf() 메서드 -
module Python - math.erfc() 메서드 -
module Python - math.exp() 메서드 -
module Python - math.expm1() 메서드 -
module Python - math.fabs() 메서드 -
module Python - math.factorial() 메서드 -
module Python - math.floor() 메서드 ★ - 바닥 반올림 (= 하위 정수로 반올림. = floor메…
module Python - math.fmod() 메서드 -
module Python - math.frexp() 메서드 -
module Python - math.fsum() 메서드 -
module Python - math.gamma() 메서드 -
module Python - math.gcd() 메서드 -
module Python - math.hypot() 메서드 -
module Python - math.isclose() 메서드 -
module Python - math.isfinite() 메서드 - 숫자 유한 여부 체크 (= math.isfinite메…
module Python - math.isinf() 메서드 -
module Python - math.isnan() 메서드 ★ - NaN 여부 체크. (= isnan메서드 = 이즈난메서…
module Python - math.isqrt() 메서드 -
module Python - math.ldexp() 메서드 -
module Python - math.lgamma() 메서드 -
20/24
목록
찾아주셔서 감사합니다. Since 2012