+ 1
Python list
b = 5 list=ââ while b<11: b = b + 2; if b%2 == 1 list=str(b)+â â + list print (list) Please show me how to get to the output in steps. Thank you.
13 Respostas
+ 1
b = 5 # Sets Value of b to Integer 5
list=ââ # Creates an emty String
while b < 11: # This Loop will continue as long as the value b is smaller than 11
b = b + 2 # Adds 2 to b. With the first Iteration b is 7
if b % 2 == 1 # Checks if b is an odd Number
list = str(b) + â â + list # Adds the current Value of b to the List if the Condition of the if-Statement is True
print (list) # Output of the final List
----
By the Way this Program makes not much sence. It starts with an odd number and adds 2 - it is logical that this must be an odd Number, too.
+ 1
Because you add str(b) + list. If you want an ascending Order write
list = list + " " + str(b)
+ 1
You can do it with a list comprehension
print([b for b in range(7, 13, 2) if b%2])
+ 1
Python will execute your Code exactly in the Order you write it down:
list = list + str(b)
means that 'b' will be appended to the existing 'list'. The result is a 'list' in ascending Order.
list = str(b) + list
means that the existing 'list' will be appended to 'b'. The Result is a List in descending Order.
+ 1
list is the Name of the Stirng you defined in Line 2.
0
You should define a List like that:
list = []
and add Items with
list.append()
https://www.sololearn.com/learn/JUMP_LINK__&&__Python__&&__JUMP_LINK/2431/
https://code.sololearn.com/cdPALq4G3C2N/?ref=app
0
I know the answer. I just need to know the steps to it.
0
Because I donât understand how the output is the output
0
Please show me steps to it.
0
@Sebastian KeBler
Thank you so much. But Iâm confused about how itâs 11 9 7 and not 7 9 11. Why is it in reverse?
0
So does it mean that âlistâ is nothing?
0
What is list? I donât know the value for list.