Please, have mercy on my poor soul. I have never done Python; before, I am learning python. I have attempted all day long day to do this.
Write a python program that iterates the integers from 1 to 50. For multiples of three print "Cloud" instead of the number For multiples of seven print "Computing" For numbers which are multiples of both three and seven print "CloudComputing"
What I've got accomplished:
x = 0
while x < 50:
print(x)
x = 1
if x % 3 == 0:
print("cloud")
if x % 7 == 0:
print("computing")
if x % 3 x % 7 == 0:
x == print("cloudcomputing")
What I get is a list of both integers and #'s how do I get python to replace the integers with the string?
& How do I set it up to follow all the rules and not print each statement.
I've literally struggled all day over this.
0
1
2
cloud
3
4
5
cloud
6
computing
7
8
cloud
9
10
11
cloud
12
13
computing
14
cloud
15
16
17
cloud
18
19
20
cloud
computing
cloudcomputing
21
22
23
cloud
24
25
26
cloud
27
computing
28
29
cloud
30
31
32
cloud
33
34
computing
35
cloud
36
37
38
cloud
39
40
41
cloud
computing
cloudcomputing
42
43
44
cloud
45
46
47
cloud
48
computing
49
CodePudding user response:
You could do it like this:
for i in range(1, 51):
if i % 3 == 0:
if i % 7 == 0:
print('CloudComputing')
else:
print('Cloud')
elif i % 7 == 0:
print('Computing')
else:
print(i)
CodePudding user response:
You need to change the order of condition matching in your loop. First check if number is multiple of BOTH 3 & 7, otherwise you'll get two print statements rather than the intended one.
x = 1
while x <= 50:
if x % 3 == 0 and x % 7 == 0:
print("cloudcomputing")
elif x % 3 == 0:
print("cloud")
elif x % 7 == 0:
print("computing")
else:
print(x)
x = 1
CodePudding user response:
You should use elif
for multiple conditions not several if
when you use multiple if
s it all if will be checked and procced but when you use elif
if one condition be true it will break whole condition and goes to next number and so on
x = 0
while x <= 50:
if x % 3 x % 7 == 0:
print("cloudcomputing")
elif x % 3 == 0:
print("cloud")
elif x % 7 == 0:
print("computing")
else:
print(str(x))
x = x 1