I can't seem to figure out how to make a program that asks for a Start input, End input, and an amount that the program counts by. My request might not be so clear so here's an example:

Start at: 1
End at: 20
Count by: 3
1 4 7 10 13 16 19

If anyone could provide some help, I'm appreciate it alot. Oh, and if possible, could you provide a solution making use of the "for" loop and another solution making use of the "while" loop. Thank you to anyone that can help.

Recommended Answers

All 8 Replies

You should try to put some code together.

Look at this,and you should figure how to set opp your variables.

IDLE 2.6.2 
>>> for i in range(1, 20, 3):
	print i   #print (i) for python 3	
1
4
7
10
13
16
19
######
#User input for python 2.x
>>> Start = int(raw_input('Enter first number: '))
Enter first number: 1
>>> Start
1
>>> type(Start)
<type 'int'>
>>> 
######
#User input for python 3.x
>>> Start = int(input('Enter first number: '))
Enter first number: 1
>>> Start
1
>>> type(Start)
<class 'int'>
>>>

Hint, I would go with a for loop since it can iterate over the function range(start, end, step). Our friend snippsat gave you a pretty good example.

Ah, thank you for help...I have one more request though, is there any way to do the same thing but with the "while" loop?

Huh, this while loop thing twice is two days. Assignments perhaps?
Yes there is, you can always go between while and for loops, its just usually for loops are easier.

for example

count = 3
while count <50:
    print(count)
    count += 3

See? Still simple,just not quite as simple :P

>>> start = 1
>>> end = 19

>>> while start < end:
	print start

#What happens here,try to think about it.
#Yes it print 1 forever,ctrl + c to break out when in IDLE.
#Because 1 is less than 19 and we do not multiply start(1)

###
>>> while start < end:
	print start
	start = start + 1  #or start += 1

#What happens here,try to think about it.
#yes we add 1 + 1, 1 + 2, 1 + 3 til we reach end(19)
#If no end(19) while loop will go on forever(Infinite Loop)

#paulthom12345 has the last part you need to figure this out.

Yeah i have a feeling there is a school assignment due soon :P

Almost looks like someone is progressive enough to teach Python in Elementary School.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.