how to? - Sajha Mobile
SAJHA MOBILE
how to?
Posts 17 · Viewed 9427 · Go to Last Post
comingsoon
· Snapshot 0
Like · Likedby · 0
Dear programmer and engineers.
Please help me how to programe this in python.

should print like this.

>>> 1-2+3=2


Thanks in advance.

alece
· Snapshot 141
Like · Liked by · 0
like this ?

print (1-2+3)

question clear bhayena bro!! ^^
chi-sigma
· Snapshot 166
Like · Liked by · 0
do you mean this?

print "1-2+3=",1-2+3
comingsoon
· Snapshot 259
Like · Liked by · 0
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
Kiddo
· Snapshot 332
Like · Liked by · 0

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.

comingsoon
· Snapshot 367
Like · Liked by · 0
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
· Snapshot 385
Like · Liked by · 0
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. 

prankster
· Snapshot 398
Like · Liked by · 0
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
Kiddo
· Snapshot 398
Like · Liked by · 0
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)

prankster
· Snapshot 437
Like · Liked by · 0
@Kiddo, just wondering won't urs result into  
1-2+3-4+5-=3
comingsoon
· Snapshot 430
Like · Liked by · 0
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
prankster
· Snapshot 465
Like · Liked by · 0
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
magorkhe1
· Snapshot 481
Like · Liked by · 0

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/


Kiddo
· Snapshot 510
Like · Liked by · 0
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.
comingsoon
· Snapshot 523
Like · Liked by · 0
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
prankster
· Snapshot 553
Like · Liked by · 0
did you put indentation for print(i)   
print("-")                                          

           print(i)

I went to http://mathcs.holycross.edu/~kwalsh/python/
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
 
comingsoon
· Snapshot 570
Like · Liked by · 0
I got the point. It's looks perfect now. Thanks much.





Please log in to reply to this post

You can also log in using your Facebook
View in Desktop
What people are reading
You might like these other discussions...
· Posts 3 · Viewed 259
· Posts 1 · Viewed 71
· Posts 19 · Viewed 3134 · Likes 1
· Posts 1 · Viewed 91
· Posts 1 · Viewed 78
· Posts 1 · Viewed 123
· Posts 1 · Viewed 133
· Posts 4 · Viewed 1141
· Posts 1 · Viewed 252
· Posts 1 · Viewed 162



Your Banner Here
Travel Partners
Travel House Nepal