简体   繁体   中英

Convert positive input into negative number

start = int(input("Please enter starting number"))
end = int(input("Please enter ending number"))
amount = int(input("Please enter the amount to count"))

for i in range(start,end,amount):
    print(i)

I want the number from the amount variable to be converted into a negative number so that I can reverse range() .

You want to negate it only if the start is greater than the end.

if start > end:
    amount = -amount

Or you can tuck that right into the range() call:

for i in range(start, end, -amount if start > end else amount):

我不确定这是否是您真正想要的,但是要将金额转换为负数,您只需键入以下内容:

amount = amount * -1

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM