New to Python

D

dnwurtz

I am trying to get a program to add up input from the user to get to
the number 100 using a loop. However, I am having some issues. Here
is what I have so far. I know I am just trying to hard, but I am
stuck. Thank you for any help.

print "We need to count to 100"

high_number = 100
total = 0

number = input("Enter your first number ")
sum = number + total
while sum < high_number:
print "Not there yet..."
number = input("Enter another number ")

print "We are there!!!"
 
S

Stargaming

I am trying to get a program to add up input from the user to get to
the number 100 using a loop. However, I am having some issues. Here
is what I have so far. I know I am just trying to hard, but I am
stuck. Thank you for any help.

print "We need to count to 100"

high_number = 100
total = 0

number = input("Enter your first number ")
sum = number + total
while sum < high_number:
print "Not there yet..."
number = input("Enter another number ")

print "We are there!!!"

Just reading the error messages would be a good start. You assign `sum`
to `number + total` but as I see it there's no `number` so far. You have
to increment some variable with the user's input. (`sum += input(..)`)
Additionally, `total` and `number` seem pretty redundant. You only need
two variables, one containing the targeted number, one the current progress.
By the way, input() is considered pretty evil because the user can enter
arbitrary expressions. It is recommended to use raw_input() (till Python
3.0), in your case e. g. int(raw_input()) wrapped in a try-except block.
HTH,
Stargaming
 
B

Bjoern Schliessmann

I am trying to get a program to add up input from the user to get
to the number 100 using a loop. However, I am having some issues.

Please, if you have a problem post the exact error symptoms.
Here is what I have so far. I know I am just trying to hard, but
I am stuck. Thank you for any help.

We can't tell by magic where you are stuck. Please state observed
and expected behaviour.
print "We need to count to 100"

high_number = 100
total = 0

number = input("Enter your first number ")

(already mentioned: please don't use input)
sum = number + total

This is a one-time assignment.
while sum < high_number:
print "Not there yet..."
number = input("Enter another number ")

print "We are there!!!"

"We" won't reach this point since the while condition never becomes
False. See above; "sum" is assigned just one time and will not
change in your while loop. Having the program work as probably
expected requires a "sum += number" inside the while loop.

BTW, what's "total" expected to do? It's also assigned only one
time.

Regards,


Björn
 
A

Arnaud Delobelle

I am trying to get a program to add up input from the user to get to
the number 100 using a loop. However, I am having some issues. Here
is what I have so far. I know I am just trying to hard, but I am
stuck. Thank you for any help.

print "We need to count to 100"

high_number = 100
total = 0

number = input("Enter your first number ")
sum = number + total
while sum < high_number:
print "Not there yet..."
number = input("Enter another number ")

print "We are there!!!"

There is no need for 'sum' and 'total'. Here's a working version of
your program. Look at the differences with your attempt.

----------
print "We need to count to 100"

high_number = 100

total = input("Enter your first number ") # first total is the fist
number
while total < high_number:
print "Not there yet..."
number = input("Enter another number ")
total = total + number # Add the new number to the total

print "We are there!!!"
----------

Good luck !

As others have pointed out, 'input' is a function to be careful with.
You could use answer=raw_input(...) and then convert the result to an
int by using number=int(answer).
 
D

dnwurtz

There is no need for 'sum' and 'total'. Here's a working version of
your program. Look at the differences with your attempt.

----------
print "We need to count to 100"

high_number = 100

total = input("Enter your first number ") # first total is the fist
number
while total < high_number:
print "Not there yet..."
number = input("Enter another number ")
total = total + number # Add the new number to the total

print "We are there!!!"
----------

Good luck !

As others have pointed out, 'input' is a function to be careful with.
You could use answer=raw_input(...) and then convert the result to an
int by using number=int(answer).


Thanks for the help guys!!!

Before I got a chance to read your answer Arnaud, I came up with the
below. Seems to work also. Yours is easier :).

print "We need to count to 100"

high_number = 100
sum = 0

sum += input("Enter your first number ")
print sum
while sum < high_number:
print "Not there yet..."
sum += input("Enter another number ")
print sum

print "We are there!!!"
 
B

Bruno Desthuilliers

(e-mail address removed) a écrit :
I am trying to get a program to add up input from the user to get to
the number 100 using a loop. However, I am having some issues. Here
is what I have so far. I know I am just trying to hard, but I am
stuck.

Where ?

May I suggest this reading ?
http://www.catb.org/~esr/faqs/smart-questions.html
Thank you for any help.

print "We need to count to 100"

high_number = 100
total = 0

number = input("Enter your first number ")

Please re-read *very carefully* the doc for input(). Then switch to
raw_input() and perform appropriate validation and conversion.
sum = number + total

don't use 'sum' as an identifier, unless you don't mind shadowing the
builtin sum() function.
while sum < high_number:
print "Not there yet..."
number = input("Enter another number ")

print "We are there!!!"

We're not. number won't be automagically added to sum.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top