How to loop in the opposite order?

2024/11/9 2:12:24

I am a beginner programmer. Here is my code:

n = int(input())
from math import*
for i in range(n):print(n, "\t", log10(n))i = i + 1n = n - 1

Its output is:

10   1.0
9    0.9542425094393249
8    0.9030899869919435
7    0.8450980400142568    
6    0.7781512503836436
5    0.6989700043360189    
4    0.6020599913279624
3    0.47712125471966244
2    0.3010299956639812
1    0.0

I want it to be:

1    0.0
2    0.3010299956639812
3    0.47712125471966244
4    0.6020599913279624
5    0.6989700043360189
.
.
.
9    0.9542425094393249
10   1.0
Answer

First, you don't need to increment i, because it is the loop variable and is being set to each of 0 to 9 in turn.

Then your loop is printing n first. It starts at 10, and you subtract one from it each time, so you are getting the values in descending order. Try this:

for i in range(n):print i+1, "\t", log10(i+1)
https://en.xdnf.cn/q/120551.html

Related Q&A

Override methods with same name in Python programming [duplicate]

This question already has answers here:Closed 12 years ago.Possible Duplicate: How do I use method overloading in Python?I am new to Python programming, and I like to write multiple methods with the …

TypeError: function object is not subscriptable in Python 3.4.3?

I have a food menu and the stock and prices are in separate dictionaries.Food Stock:Food_Stock = {Chips : 15,Bagels : 27,Cookies : 25}#Food Stock.Food Prices:Food_Prices = {#Food Prices.Chips : 1,Bagel…

Insert a value in date format dd-mm-yyyy in dictionary in python

I am creating a dictionary having some values including a date of birth of a person. But when I run my code, it is giving an error "datetime.datetime has no attribute datetime" . Here is my …

Collisions arent registering with Python Turtles

Im creating a simple 2D shooter following an online tutorial, and the enemy sprites (except 1, there are 5 total) are not abiding by my collision code. Everything works except the bullet object that th…

Function should clean data to half the size, instead it enlarges it by an order of magnitude

This has been driving me nuts all week weekend. I am trying merge data for different assets around a common timestamp. Each assets data is a value in dictionary. The data of interest is stored in lists…

is it possible to add colors to python output? [duplicate]

This question already has answers here:How do I print colored text to the terminal?(66 answers)Closed 10 years ago.so i made a small password strength tester for me, my friends and my family, as seen …

Tkinter Function attached to Button executed immediately [duplicate]

This question already has answers here:How can I pass arguments to Tkinter buttons callback command?(2 answers)Closed 8 years ago.What I need is to attach a function to a button that is called with a …

Error!!! cant concatenate the tuple to non float

stack = []closed = []currNode = problem.getStartState()stack.append(currNode)while (len(stack) != 0):node = stack.pop()if problem.isGoalState(node):print "true"closed.append(node)else:child =…

Using R to fit data from a csv with a gamma function?

Using the following data: time_stamp,secs,count 2013-04-30 23:58:55,1367366335,32 2013-04-30 23:58:56,1367366336,281 2013-04-30 23:58:57,1367366337,664 2013-04-30 23:58:58,1367366338,1255 2013-04-30…

regex multiple string match in a python list

I have a list of strings like this:["ra", "dec", "ra-error", "dec-error", "glat", "glon", "flux", "l", "b"]I ne…