how to?

Archived from the original Sajha.com — preserved as posted, replies can no longer be added here.
Start a New Discussion
Archived Post

Dear programmer and engineers. Please help me how to programe this in python. should print like this. >>> 1-2+3=2 Thanks in advance.

comingsoon · Feb 6, 2013 12:22 AM · 20,507 views

16 Replies

like this ? print (1-2+3) question clear bhayena bro!! ^^

alece · Feb 6, 2013 9:43 AM

do you mean this? print "1-2+3=",1-2+3

chi-sigma · Feb 6, 2013 10:24 AM

I try this but total is not right.  also 1-2+3-4  doesn't work while user give odd number. N = int(input("N: ")) i=1 x=2 Sum1=0 Sum2=0 while i <= N and x <=N:     print("+", i, "-", x, end=' ')     i= i+2     x= x+2     Sum1=Sum1+i     Sum2=Sum2+x print("=", Sum1-Sum2) Result >>> N: 4 + 1 - 2 + 3 - 4 = -2 >>> ================================ RESTART ================================ >>> N: 5 + 1 - 2 + 3 - 4 = -2 >>>  Here it prints only 4, should print upto 5(user's given value) and total is not valid. Last edited: 06-Feb-13 12:50 PM

comingsoon · Feb 6, 2013 12:42 PM

You have a flaw in your logic I am not familiar with Python programming and am no longer a programmer, but it seems fairly straight forward so I tested your logic and you are getting what you are programming. Flaw First of all, how can you print two variables at a time (i and x with print function) when you might have to print odd number of variables (say N=5)? WIth you program you can only do 1-2+3-4+5-6 and so on, not 1-2+3 OR 1-2+3-4+5. Second, for N=5, your program will NOT run beyond those 4 since X becomes 6 which stops the program from going more than 2 iterations, hence 4 values.          Let's check it out (for N=5)                First iteration: x (initial value) = 2, x (final value)=4 //Since x=x+2                Second iteration: x (initial value) = 4, x (final value)=6                Third iteration fails as x>N Solution First of all, you can't print two variables at once since you can have odd number of digits (like N=5). But, if you print one at a time, you have to alternate between + and -. You can do this in a number of ways, let's keep it simple and use i as our counter. So if i is odd (i/2 not=1) then you will print "-" and add to the sum while if it is even you will do opposite: N = int(input("N: ")) i=1 Sum=0 while i <= N:          if i/2=1:                                              //If i is even            print(i, "+", end=' ')            Sum=Sum-i     else:                                                  //if i is odd            print(i, "-", end=' ')            Sum=Sum+i      i=i+1 print("=", Sum) Like I said, I am not familiar with Python and might have my syntax wrong, but the logic should work.

Kiddo · Feb 6, 2013 3:00 PM

Thanks kiddo, I'm now almost in the solutin. there is little thing to correct. Code: N = int(input("N: ")) i=1 Sum1=0 Sum2=0 while i <= N:         if i%2==0:                                              #If i is even            print("-",i, end=' ')            Sum1=Sum1+i     else:                                                  #if i is odd            print("+", i, end=' ')            Sum2=Sum2+i     i=i+1     print("=", Sum2-Sum1) Result: N: 5 + 1 - 2 + 3 - 4 + 5 = 3 >>> ================================ RESTART ================================ >>> N: 3 + 1 - 2 + 3 = 2 >>> ================================ RESTART ================================ >>> N: 4 + 1 - 2 + 3 - 4 = -2 How to remove + sign before 1 should look like this 1-2+3-4=-2

comingsoon · Feb 6, 2013 3:40 PM

Thanks everyone, specially thanks kiddo for your help. I got a solution printing 1 before the loop and starting counter i=2, But I feel like very Tori Method . if someone have different idea please suggest me, I wanna learn. 

comingsoon · Feb 6, 2013 3:55 PM

N = int(input("N: ")) i=1 Sum=0 while i <= N:          if i%2==0:              if (i !=1)                print("-")                                          #If i is even            print(i)                          Sum=Sum-i     else:               if (i !=1)                 print("+")                                               #if i is odd            print(i)            Sum=Sum+i     i=i+1      print("=", Sum) Last edited: 06-Feb-13 03:56 PM

prankster · Feb 6, 2013 3:55 PM

While changing the syntax (i%2 instead of i/2) is understandable, I don't know why you changed the logic? If you keep my logic intact, you should get it the way you want it, i.e 1-2+3-4+5=3 N = int(input("N: ")) i=1 Sum=0 while i <= N:         if i%2==0:                                              #If i is even            print(i, "+", end=' ')            Sum=Sum-i     else:                                                  #if i is odd            print(i, "-", end=' ')            Sum=Sum+i     i=i+1     print("=", Sum)

Kiddo · Feb 6, 2013 4:01 PM

@Kiddo, just wondering won't urs result into   1-2+3-4+5-=3

prankster · Feb 6, 2013 4:08 PM

Sorry kiddo, there was some syntax error so I modifed and came to my way. I am using python 3.2.3. There is extra sign at last of the series. Here is result of your last code. N: 5 1 - 2 + 3 - 4 + 5 - = 3 >>> ================================ RESTART ================================ >>> N: 4 1 - 2 + 3 - 4 + = -2 >>> prankster, your code don't render 1 at begining and series start from - sign, same problem as we discuss here. Here is the result. >>> ================================ RESTART ================================ >>> N: 5 - 2 + 3 - 4 + 5 = 2 >>> ================================ RESTART ================================ >>> N: 4 - 2 + 3 - 4 = -3

comingsoon · Feb 6, 2013 4:22 PM

I'm not familiar with python either,  following if should only be for   print("-")  and not print(i) . and i assume, end=' ' is for not adding line break. if (i !=1)             print("-") N = int(input("N: ")) i=1 Sum=0 while i <= N:          if i%2==0:              if (i !=1):                print("-")                                          #If i is even            print(i)                          Sum=Sum-i     else:               if (i !=1):                 print("+")                                               #if i is odd            print(i)            Sum=Sum+i     i=i+1      print("=", Sum)   probably that would work, pls add end = ' ' to all the print statement

prankster · Feb 6, 2013 4:29 PM

EDX.ORG has many online courses.  MIT is offering Python and it started yesterday. Anybody interested can sign any time and any courses. https://www.edx.org/

maGorkhe1 · Feb 6, 2013 4:40 PM

You both are right, it will have the sign at the end. I am done for today and will look at it tomorrow in case prankster's doesn't work.

Kiddo · Feb 6, 2013 4:45 PM

prankstar there is still same problem, I modefied little bit for syntax error here is result. >>> ================================ RESTART ================================ >>> N: 7 - 2 + 3 - 4 + 5 - 6 + 7 = 3 >>> I'm not worried for same line but 1 is missing from begining. I am still try to modify if i can get that 1 on begining. Thanks Last edited: 06-Feb-13 05:08 PM

comingsoon · Feb 6, 2013 5:07 PM

did you put indentation for print(i)    print("-")                                                      print(i) I went to http://mathcs.holycross.edu/~k... and tried following code,  N = 5 i=1 Sum=0   while i <= N:          if i%2==0:              if (i !=1):               print "-"                                                    print i                         Sum=Sum-i     else:               if (i !=1):                 print "+"                                                        print i           Sum=Sum+i     i=i+1      print "=" + str(Sum)  The output was,  1 - 2 + 3 - 4 + 5 =3  

prankster · Feb 6, 2013 5:33 PM

I got the point. It's looks perfect now. Thanks much.

comingsoon · Feb 6, 2013 5:53 PM

This conversation is preserved exactly as it was on the original Sajha.com and can't accept new replies.

Start a New Discussion

You might be interested in...

Recent Classifieds View all
Upcoming Events View all
Service Providers View all