A prime number is an integer greater than 1 that is evenly divisible by only 1 and itself. For example, the number 5 is prime because it can only be evenly divided by 1 and 5. The number 6, however, is not prime because it can be divided by 1, 2, 3, and 6.
Write a function named isPrime, which takes an integer as an argument and returns True if the argument is a prime number, and False otherwise.
Define a function main() and call isPrime() function in main() to display a list of the prime numbers from 100 to 500.
Program (prime.py)
def isPrime(number):
for i in range(2,number):
if number % i == 0:
return False
return True
def main():
for n in range(100,501):
if isPrime(n):
print(n, end=' ')
main()
Output
101 103 107 109 113 127 131 137 139 149 151 157 163 167 173 179 181 191 193 197 199 211 223 227 229 233 239 241 251 257 263 269 271 277 281 283 293 307 311 313 317 331 337 347 349 353 359 367 373 379 383 389 397 401 409 419 421 431 433 439 443 449 457 461 463 467 479 487 491 499
Write a program that lets the user perform arithmetic operations on two numbers. Your program must be menu driven, allowing the user to select the operation (+, -, *, or /) and input the numbers. Furthermore, your program must consist of following functions:
1. Function showChoice: This function shows the options to the user and explains how to enter data.
2. Function add: This function accepts two number as arguments and returns sum.
3. Function subtract: This function accepts two number as arguments and returns their difference.
4. Function mulitiply: This function accepts two number as arguments and returns product.
5. Function divide: This function accepts two number as arguments and returns quotient.
Define a function main() and call functions in main().
Program (menu.py)
def show_choices():
print('\nMenu')
print('1. Add')
print('2. Subtract')
print('3. Multiply')
print('4. Divide')
print('5. Exit')
def add(a, b):
return a + b
def subtract(a, b):
return a - b
def multiply(a, b):
return a * b
def divide(a, b):
return a / b
def main():
while(True):
show_choices()
choice = input('Enter choice(1-5): ')
if choice == '1':
x = int(input('Enter first number: '))
y = int(input('Enter second number: '))
print('Sum =', add(x, y))
elif choice == '2':
x = int(input('Enter first number: '))
y = int(input('Enter second number: '))
print('Difference =', subtract(x, y))
elif choice == '3':
x = int(input('Enter first number: '))
y = int(input('Enter second number: '))
print('Product =', multiply(x, y))
elif choice == '4':
x = int(input('Enter first number: '))
y = int(input('Enter second number: '))
if y == 0:
print('Error!! divide by zero')
else:
print('Quotient =', divide(x, y))
elif choice == '5':
break
else:
print('Invalid input')
main()
Output
Menu 1. Add 2. Subtract 3. Multiply 4. Divide 5. Exit Enter choice(1-5): 1 Enter first number: 10 Enter second number: 14 Sum = 24 Menu 1. Add 2. Subtract 3. Multiply 4. Divide 5. Exit Enter choice(1-5): 3 Enter first number: 56 Enter second number: 4 Product = 224 Menu 1. Add 2. Subtract 3. Multiply 4. Divide 5. Exit Enter choice(1-5): 5 >>>