[Show all top banners]

comingsoon
Replies to this thread:

More by comingsoon
What people are reading
Subscribers
:: Subscribe
Back to: Kurakani General Refresh page to view new replies
 how to?
[VIEWED 5649 TIMES]
SAVE! for ease of future access.
Posted on 02-06-13 12:22 AM     Reply [Subscribe]
Login in to Rate this Post:     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.


 
Posted on 02-06-13 9:43 AM     [Snapshot: 141]     Reply [Subscribe]
Login in to Rate this Post:     0       ?    
 

like this ?

print (1-2+3)

question clear bhayena bro!! ^^
 
Posted on 02-06-13 10:24 AM     [Snapshot: 166]     Reply [Subscribe]
Login in to Rate this Post:     0       ?    
 

do you mean this?

print "1-2+3=",1-2+3

 
Posted on 02-06-13 12:42 PM     [Snapshot: 259]     Reply [Subscribe]
Login in to Rate this Post:     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

 
Posted on 02-06-13 3:00 PM     [Snapshot: 332]     Reply [Subscribe]
Login in to Rate this Post:     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.


 
Posted on 02-06-13 3:40 PM     [Snapshot: 367]     Reply [Subscribe]
Login in to Rate this Post:     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





 
Posted on 02-06-13 3:55 PM     [Snapshot: 385]     Reply [Subscribe]
Login in to Rate this Post:     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. 


 
Posted on 02-06-13 3:55 PM     [Snapshot: 398]     Reply [Subscribe]
Login in to Rate this Post:     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

 
Posted on 02-06-13 4:01 PM     [Snapshot: 398]     Reply [Subscribe]
Login in to Rate this Post:     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)


 
Posted on 02-06-13 4:08 PM     [Snapshot: 437]     Reply [Subscribe]
Login in to Rate this Post:     0       ?    
 

@Kiddo, just wondering won't urs result into  
1-2+3-4+5-=3
 
Posted on 02-06-13 4:22 PM     [Snapshot: 430]     Reply [Subscribe]
Login in to Rate this Post:     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

 
Posted on 02-06-13 4:29 PM     [Snapshot: 465]     Reply [Subscribe]
Login in to Rate this Post:     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

 
Posted on 02-06-13 4:40 PM     [Snapshot: 481]     Reply [Subscribe]
Login in to Rate this Post:     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/



 
Posted on 02-06-13 4:45 PM     [Snapshot: 510]     Reply [Subscribe]
Login in to Rate this Post:     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.
 
Posted on 02-06-13 5:07 PM     [Snapshot: 523]     Reply [Subscribe]
Login in to Rate this Post:     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

 
Posted on 02-06-13 5:33 PM     [Snapshot: 553]     Reply [Subscribe]
Login in to Rate this Post:     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
 

 
Posted on 02-06-13 5:53 PM     [Snapshot: 570]     Reply [Subscribe]
Login in to Rate this Post:     0       ?    
 

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






 


Please Log in! to be able to reply! If you don't have a login, please register here.

YOU CAN ALSO



IN ORDER TO POST!




Within last 365 days
Recommended Popular Threads Controvertial Threads
What stocks to buy -- Any recommendation
TRUMP 2016!!! Here is why?
हेरेको तिमीलाइ आँखाले हैन।
जोडि त भाग्यले जुराउँछ - हिरोइन स्वेताले यसरि दोश्रो पटक बिहे गर्न लागिन
ॐ श्री स्वस्थानी ब्रत कथा
लौ .. संसद नै बिघठन गरिदिएछन ओलिबाले
Conservative discussions
उस्तै उस्तै हामी
Looking for Potential Match
निर्लज्जताको बाढी आएको छ
Love Letters From The Past--SITARA
हाकिमनी संग- आज सुक्रबार -३
सान भिनाजु लाई अझै पैसो कमाउने आइडिया
Ushering in new era of communism ...
Rain Rain .. Raut
I m offered a Job in Bank in Nepal, should i go back ?
श्राद्द
अन्तरद्वन्द
Fox News: Wear the Damn Mask, it is not political issue but a public health issue.
Photo of the day
Nas and The Bokas: Coming to a Night Club near you
विद्या तेरो सिन्दूर खोइ, केपी ओली तेरो पोइ ..
TRUMP 2016!!! Here is why?
NOTE: The opinions here represent the opinions of the individual posters, and not of Sajha.com. It is not possible for sajha.com to monitor all the postings, since sajha.com merely seeks to provide a cyber location for discussing ideas and concerns related to Nepal and the Nepalis. Please send an email to admin@sajha.com using a valid email address if you want any posting to be considered for deletion. Your request will be handled on a one to one basis. Sajha.com is a service please don't abuse it. - Thanks.

Sajha.com Privacy Policy

Like us in Facebook!

↑ Back to Top
free counters