Can someone give me advise or just change my code??Help!

I am a newbie like EXTREMELY new kind of newbie also I have no idea what the problem is because there are no errors that python is recognizing
So basically, I wanted to make a code that

  1. Asks what number to start at
  2. Asks what number to end at
    3.Asks what to count up in
    4.Prints the list of numbers
    5.Prints the sum of the numbers in the list
no = int(input("What number should we start with?: "))
no1 = int(input("What number should we end with?: "))
no2 = int(input("What number should we count by?: "))
print("This is your list of numbers:")

i2=no
while i2<= no1:
    print(i2,end= " ")
    i2=i2+no2
print()
print("Sum of numbers:")
s=0
while s<= no1:
    print(s,end="")
    s=s+no2
    if s>no1*no2:
        break

The first thing you should explain is: why do you believe there is a problem?

That is to say: what happens when the code runs, and how is that different from what you want it to do?

The next thing is to explain, step by step, how you intend for the code to work. What do you think each part means, and how does it relate to the task description?

Then, think carefully about the problem description. You say that the program should “print the sum of the numbers in the list”. So - what list? Do you actually want to make a list of them, or do you want to add them up “as you go”?

3 Likes

Also take into account that Python has built-in functions that do most of what you need.

range(no, no1 + 1, no2 ) generates the numbers from no, not larger than no1 at intervals of no2.

list(range(no, no1 + 1, no2)) would give a list of them.

sum(range(no, no1 + 1, no2)) would give their sum.

Whether they expect you to use them or not, you might need to ask.

Evidently you want to generate a sequence of numbers, and secondly add those numbers up. What you have here is basically right. Yes @franklinvp there are much cleverer ways, but I suggest that’s for later.

@Yoda am I right in thinking that the first loop correctly prints the numbers you wanted?

Then the only thing that is actually wrong here is that the second loop doesn’t generate the same numbers as the first loop. It would work if you just copied the same logic:

i2=no
while i2<= no1:
    i2=i2+no2

but then each time it comes up with a new i2 you add that into s.

Then when you look at it, you will probably say “Hey, why am I generating the same numbers all over again” and realise you could have computed the sum in the first loop (which is what @kknechtel hints at).

Are you saying those very basic range usages are"clever"? :slight_smile: (I only see one somewhat clever thing we could do here, maybe we’ll get there. But for now I agree with Karl that Mr. Star Trek should tell us why they even think there’s a problem before we do anything.)

At the level the OP has invited input, yes. Built-ins with iterable arguments are magic at this stage. The bug can be fixed using the understanding already demonstrated.

Fair point this would be better question if the OP showed the output and said why it was “obviously wrong”. I think we know without running it, but I could be mistaken.

Star Wars. (You knew that.) :slight_smile: