|
| 1 | + |
| 2 | +# coding: utf-8 |
| 3 | + |
| 4 | +# It is assumed that a software developer is asked to write a Python program that can calculate and print out the diameter and circumference of a circle. The user enters data of the radius and its measurement unit(in, ft,cms or m) from the console |
| 5 | + |
| 6 | +# In[ ]: |
| 7 | + |
| 8 | + |
| 9 | +pi=3.14159 |
| 10 | +radius=float(input("Enter radius: ")) |
| 11 | +if (radius<0): |
| 12 | + print("This is an error. Enter radius greater than 0") |
| 13 | + radius=float(input("Enter radius: ")) |
| 14 | +unit=input("Enter units: ") # unit: cms,inches,ft,m |
| 15 | +if(unit is not among (inches,ft,cms,m): |
| 16 | + unit=input("Enter the correct unit from the list: ") |
| 17 | + |
| 18 | +diameter= 2*radius |
| 19 | +circumference=diameter * pi |
| 20 | +print("Diameter: ",diameter,unit) |
| 21 | +print("Circumference: ",circumference,unit) |
| 22 | + |
| 23 | + |
| 24 | +# ### Calculating the diameter and circumference of a circle if a user makes mistakes while entering the radius again and again |
| 25 | +# |
| 26 | +# The Program must repeatedly check the input until it gets the correct one-> The program uses LOOPS |
| 27 | + |
| 28 | +# In[1]: |
| 29 | + |
| 30 | + |
| 31 | +# while LOOP |
| 32 | +pi=3.14159 |
| 33 | +radius=float(input("Enter radius: ")) |
| 34 | +while(radius<0): |
| 35 | + print("This is an error. Enter radius greater than 0") |
| 36 | + radius=float(input("Enter radius: ")) |
| 37 | +unit=input("Enter units: ") # unit: cms,inches,ft,m |
| 38 | +diameter= 2*radius |
| 39 | +circumference=diameter * pi |
| 40 | +print("Diameter: ",diameter,unit) |
| 41 | +print("Circumference: ",circumference,unit) |
| 42 | + |
| 43 | + |
| 44 | +# In[2]: |
| 45 | + |
| 46 | + |
| 47 | +# if-else |
| 48 | +numCredits = int(input("Enter # of credits then press Enter:")) |
| 49 | + |
| 50 | +if(numCredits >= 120): |
| 51 | + readyToGraduate = True; |
| 52 | +else: |
| 53 | + readyToGraduate = False; |
| 54 | + |
| 55 | +print(readyToGraduate) |
| 56 | + |
| 57 | + |
| 58 | +# In[3]: |
| 59 | + |
| 60 | + |
| 61 | +# for Loop |
| 62 | +language=["C","C++","Java","Python","Perl","Ruby","Scala"] |
| 63 | + |
| 64 | +for x in language: |
| 65 | + print(x) |
| 66 | + |
| 67 | + |
| 68 | +# ### Break Statement |
| 69 | +# Loop will be exited. The program flow will continue following the FOR LOOP, if there is any at all |
| 70 | + |
| 71 | +# In[4]: |
| 72 | + |
| 73 | + |
| 74 | +edibles=["ham","spam","eggs","nuts"] |
| 75 | +for food in edibles: |
| 76 | + if food== "spam": |
| 77 | + print("No More spam please!") |
| 78 | + break |
| 79 | + print("Great,delicious " + food) |
| 80 | +else: |
| 81 | + print("I'm so glad; No spam!") |
| 82 | +print("Finally, I finished stuffing myself") |
| 83 | + |
| 84 | + |
| 85 | + |
| 86 | + |
| 87 | + |
| 88 | +# In[6]: |
| 89 | + |
| 90 | + |
| 91 | +#when no break,FOR loop iterated through the whole sequence.The output is misleading since spam is in sequence |
| 92 | +edibles=["ham","spam","eggs","nuts"] |
| 93 | +for food in edibles: |
| 94 | + print("No break in FOR loop statements") |
| 95 | +else: |
| 96 | + print("I'm so glad; No spam!") |
| 97 | + |
| 98 | + |
| 99 | + |
| 100 | +# In[9]: |
| 101 | + |
| 102 | + |
| 103 | +# when no else |
| 104 | +edibles=["ham","spam","eggs","nuts"] |
| 105 | +spam=False |
| 106 | + |
| 107 | +for food in edibles: |
| 108 | + if food== "spam": |
| 109 | + spam=True |
| 110 | + print("No More spam please!") |
| 111 | + break |
| 112 | + print("Great,delicious " + food) |
| 113 | + |
| 114 | +if(not spam): |
| 115 | + print("I'm so glad; No spam!") |
| 116 | + |
| 117 | +print("Finally, I finished stuffing myself") |
| 118 | + |
| 119 | + |
| 120 | +# In[10]: |
| 121 | + |
| 122 | + |
| 123 | +# no spam |
| 124 | +edibles=["ham","eggs","nuts"] |
| 125 | +spam=False |
| 126 | + |
| 127 | +for food in edibles: |
| 128 | + if food== "spam": |
| 129 | + spam= True |
| 130 | + print("No More spam please!") |
| 131 | + break |
| 132 | + print("Great,delicious " + food) |
| 133 | + |
| 134 | +if(not spam): |
| 135 | + print("I'm so glad; No spam!") |
| 136 | + |
| 137 | +print("Finally, I finished stuffing myself") |
| 138 | + |
| 139 | + |
| 140 | +# In[12]: |
| 141 | + |
| 142 | + |
| 143 | +# Break is to terminate a loop - completely get out of the loop, immediately |
| 144 | +numItems=0 |
| 145 | +totalSales=0 |
| 146 | +totalSoldItems=0 |
| 147 | + |
| 148 | +while(numItems<totalSoldItems): |
| 149 | + price=int(input("Enter the price of the next sold item: ")) |
| 150 | + totalSales=totalSales+price |
| 151 | + if(totalSales>=1000000): |
| 152 | + break; |
| 153 | + numItems=numItems+1 |
| 154 | +print(totalSales) |
| 155 | + |
| 156 | + |
| 157 | +# In[1]: |
| 158 | + |
| 159 | + |
| 160 | +edibles=["ham","spam","eggs","nuts"] |
| 161 | +spam=False |
| 162 | + |
| 163 | +for food in edibles: |
| 164 | + if food== "spam": |
| 165 | + spam=True |
| 166 | + print("No More spam please!") |
| 167 | + continue |
| 168 | + print("Great,delicious " + food) |
| 169 | + |
| 170 | +if(not spam): |
| 171 | + print("I'm so glad; No spam!") |
| 172 | + |
| 173 | +print("Finally, I finished stuffing myself") |
| 174 | + |
0 commit comments