# Counting up to a number (Error: Repeating same number) Beginner

• (2 Pages)
• 1
• 2

## 19 Replies - 7063 Views - Last Post: 15 April 2011 - 04:43 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=227629&amp;s=59d9575fe12ad856ff68b4bb4cc2c595&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 Mossypne

Reputation: 7
• Posts: 133
• Joined: 18-August 10

# Counting up to a number (Error: Repeating same number) Beginner

Posted 14 April 2011 - 07:48 AM

Hey, Im currently learning Python using a website and after while loops it has set a task to:

Quote

1. Design a program that gives a menu for counting down and counting up to a user specified number (extra credit if you make count up and count down a submenu), also have an option that gives the output of the fibonacci sequence (0, 1, 1, 2, 3, 5, 8, 13, 21--the next number in the sequence is always the sum of the two numbers before it), and then give an option to quit of course!

Im stuck on the counting up section.

My code is:
```print "**********While Practice**********"
option = input("1. For counting\n" "2. For Fibonacci sequence\n" "Enter: ")

if option == 1:
number = input("Enter a number: ")
a = 1

while a <= number:
print a + 1

```

When ran and i enter 10 as the number it just displays 2's?? I know why it is doing this i just need help fixing it. Please dont give me the correct little bit just say what needs to be done so i can figure it out.

Any help?

Is This A Good Question/Topic? 0

## Replies To: Counting up to a number (Error: Repeating same number) Beginner

### #2 Simown

• Blue Sprat

Reputation: 322
• Posts: 650
• Joined: 20-May 10

## Re: Counting up to a number (Error: Repeating same number) Beginner

Posted 14 April 2011 - 07:58 AM

Hi, Mossypne.

You have the right idea but your logic is a little wrong.

Take this section:

```number = input("Enter a number: ")
a = 1
while a <= number:
print a + 1

```

If I enter 10 for number.

a = 1
a <= number - yes
Now, what is a + 1? 2
Loop again
a = 1
a <= number - yes
Again, what is a + 1? It's 2!

You are printing out 1 + 1 every time in the while loop, the variable a needs to be incremented as it is tested in the while loop.

The correct code is in the spoiler if you need to look at it or check your answer later

Spoiler

Does it make sense now? Hope that helps

This post has been edited by Simown: 14 April 2011 - 08:02 AM

### #3 Mossypne

Reputation: 7
• Posts: 133
• Joined: 18-August 10

## Re: Counting up to a number (Error: Repeating same number) Beginner

Posted 14 April 2011 - 08:02 AM

Yeah thanks that makes sense.

But whilst waiting for replies, i decided to have another go.

I changed my while to for and got sort of the same in.

```print "**********While Practice**********"
option = input("1. For counting\n" "2. For Fibonacci sequence\n" "Any key to quit!\n" "Enter: ")

if option == 1:
number = input("Enter a number: ")
a = 1

for x in range(1, number):
print x

elif option == 2:
print "test"

else:
quit

```

Except it never went up to 10( I know about it going one before etc.)

Thanks again.

### #4 Simown

• Blue Sprat

Reputation: 322
• Posts: 650
• Joined: 20-May 10

## Re: Counting up to a number (Error: Repeating same number) Beginner

Posted 14 April 2011 - 08:06 AM

range(x, y) is inclusive of x but exclusive of y meaning:

range(1, 10) will print 1, 2, 3, 4, 5, 6, 7, 8, 9
range(1, 11) will print 1, 2, 3, 4, 5, 6, 7, 8, 9, 10

Is that the problem you're having?

This post has been edited by Simown: 14 April 2011 - 08:07 AM

### #5 Mossypne

Reputation: 7
• Posts: 133
• Joined: 18-August 10

## Re: Counting up to a number (Error: Repeating same number) Beginner

Posted 14 April 2011 - 08:11 AM

No i have it sorted now, the for statement i used was a wild guess.

Any chance you could give me a push in the write direction for the Fibonacci sequence.

### #6 atraub

• Pythoneer

Reputation: 828
• Posts: 2,236
• Joined: 23-December 08

## Re: Counting up to a number (Error: Repeating same number) Beginner

Posted 14 April 2011 - 08:12 AM

In your second program, a is meaningless. You don't do anything with it.

As for fib, give it a shot first. You are learning, right? Let us know if you hit a snag.

This post has been edited by atraub: 14 April 2011 - 08:13 AM

### #7 Simown

• Blue Sprat

Reputation: 322
• Posts: 650
• Joined: 20-May 10

## Re: Counting up to a number (Error: Repeating same number) Beginner

Posted 14 April 2011 - 08:14 AM

What atraub said

Do you know how to generate Fibonacci numbers?

P.S. Nice editing while I was posting

This post has been edited by Simown: 14 April 2011 - 08:15 AM

### #8 Mossypne

Reputation: 7
• Posts: 133
• Joined: 18-August 10

## Re: Counting up to a number (Error: Repeating same number) Beginner

Posted 14 April 2011 - 08:24 AM

Yeah the next number is the sum of the previous two.

Also i googled it and got this:

```a = 0
b = 1
count = 0
max_count = 20

while count < max_count:
count = count + 1
aa = a
bb = b
a = bb
b = aa + bb

print(aa,)
print()
```

Im not sure why this works.

This post has been edited by Mossypne: 14 April 2011 - 08:25 AM

### #9 atraub

• Pythoneer

Reputation: 828
• Posts: 2,236
• Joined: 23-December 08

## Re: Counting up to a number (Error: Repeating same number) Beginner

Posted 14 April 2011 - 01:04 PM

If you don't get the code, don't use it!

Just think about the problem logically, to find a given number in the fibonacci sequence, all you really need to do is know the previous two, right?

### #10 Mossypne

Reputation: 7
• Posts: 133
• Joined: 18-August 10

## Re: Counting up to a number (Error: Repeating same number) Beginner

Posted 14 April 2011 - 01:07 PM

Yeah, i just cant think of a way of getting it to do that.

Ah well. Ill come back to this one tomorrow. See if stepping away from it helps.

### #11 atraub

• Pythoneer

Reputation: 828
• Posts: 2,236
• Joined: 23-December 08

## Re: Counting up to a number (Error: Repeating same number) Beginner

Posted 14 April 2011 - 01:08 PM

as a suggestion, write out how you would calculate the fibonacci sequence as a series of steps. DO NOT use any programming terminology. Instead, write it as if the person who is reading it doesn't know how to program. I suggest using bullets.

This post has been edited by atraub: 14 April 2011 - 01:10 PM

### #12 Mossypne

Reputation: 7
• Posts: 133
• Joined: 18-August 10

## Re: Counting up to a number (Error: Repeating same number) Beginner

Posted 14 April 2011 - 01:19 PM

Done that.

### #13 atraub

• Pythoneer

Reputation: 828
• Posts: 2,236
• Joined: 23-December 08

## Re: Counting up to a number (Error: Repeating same number) Beginner

Posted 14 April 2011 - 01:20 PM

Mind sharing?

Googlers may stumble across this post and if my technique works for you, they'll all say eureka!

This post has been edited by atraub: 14 April 2011 - 01:21 PM

### #14 Mossypne

Reputation: 7
• Posts: 133
• Joined: 18-August 10

## Re: Counting up to a number (Error: Repeating same number) Beginner

Posted 14 April 2011 - 01:26 PM

On my paper it just looks like this.

0, 1
0 + 1 = 1
0, 1, 1
1 + 1 = 2
0, 1, 1, 2
1 + 2 = 3
0, 1, 1, 2, 3
2 + 3 = 5
0, 1, 1, 2, 3, 5
3 + 5 = 8
0, 1, 1, 2, 3, 5, 8
5 + 8 = 13
0, 1, 1, 2, 3, 5, 8, 13
8 + 13 = 21
0, 1, 1, 2, 3, 5, 8, 13, 21

(Next number is the sum of the previous 2)

This post has been edited by atraub: 15 April 2011 - 04:29 AM
Reason for edit:: fixed minor c&p error

### #15 atraub

• Pythoneer

Reputation: 828
• Posts: 2,236
• Joined: 23-December 08

## Re: Counting up to a number (Error: Repeating same number) Beginner

Posted 14 April 2011 - 01:27 PM

Ahh but you see, that's not really what I meant.

Let's say you're talking to someone who doesn't know what a Fibonacci sequence is. How would you tell him how to calculate it? The guy you're talking to isn't going to understand unless you give him instructions... examples won't cut it for him.

This post has been edited by atraub: 14 April 2011 - 01:28 PM