2014-06-24 3 views
0

안녕하세요 저는 파이썬과 프로그래밍에 익숙하지 않습니다. while 루프를 사용하여 입력 한 숫자에 1부터 정수를 더하는 프로그램을 작성하려고합니다. 프로그램은 또한 사용자가 0 또는 음수를 입력하면 오류 문을 제공해야합니다. 지금까지 정수가 합산되고 오류 문은 작동하지만 프로그램이 루핑되지 않으면 숫자 입력 만 요청합니다. 도와주세요. 지금까지 내 소스 코드입니다. 감사합니다파이썬 프로그래밍 While 루프

x = int(input("Enter a positive number not including zero:")) 

total = 0 
n = 1 


while n <= x: 
    total = total + n 
    n = n + 1 

# prints the total of integers up to number entered 
    print("Sum of integers from 1 to number entered= ",total) 

if x <= 0 or x == -x: 
    print ("invalid entry") 
+0

을 넣으면 X <= 0 :'...'X = = -x'는'0'을 제외하고 항상 false를 반환 할 것입니다 ... – Dair

+0

'sum (range (n + 1))'을 생각해 보셨습니까? – Samba

답변

0

이 코드를 시도 이런 식으로 ...

op='y' 
while op=='y': 

     x = int(input("Enter a positive number not including zero:")) 

     total = 0 
     n = 1 

     if x > 0: 

       while n <= x: 
         total = total + n 
         n = n + 1 

         # prints the total of integers up to number entered 
         print("Sum of integers from 1 to number entered= ",total) 
     else: 
       print ("invalid entry") 


     op = raw_input("Are you want to continue this operation (y/n):") 
+0

도움을 주셔서 감사합니다 op 항목이 차이를 만들었습니다 – user3769455

+0

이것이 정말 도움이된다면, pls 내 ans 투표 ... – Jothimani

0

는 전체 코드에만`필요

done = False 
    while not done: 
     //your entire code here except the last 2 lines 
     if x > 0: 
      done = True