def displayMenu(options): for i in range(len(options)): print("{:d}. {:s}".format(i + 1, options[i])) choice = 0 while not (np.any(choice == np.arange(len(options)) + 1)): choice = inputNumber("Please choose a menu item: ") return choice
def displayMenu(options): # Viser menuen for i in range(len(options)): print("{:d}. {:s}".format(i+1, options[i])) # For at sikre at den valgte mulighed er gyldig. choice = 0 while not(np.any(choice == np.arange(len(options))+1)): choice = inputNumber("Vælg venligst en handling: ") return choice
def primeFactors(num): factors = [] i = 2 if num < 2: while num < 2: print("Invalid Input. Enter number equal or more than 2\n") num = inputNumber("Enter Number(default is 10): ", default=10) while i <= num: if (num % i) == 0: factors.append(i) num = num / i else: i += 1 return factors
def displayMenu(options): # DISPLAYMENU Displays a menu of options, ask the user to choose an item # and returns the number of the menu item chosen. # # Usage: choice = displayMenu(options) # # Input options Menu options (array of strings) # Output choice Chosen option (integer) # # Author: Mikkel N. Schmidt, [email protected], 2015 # Display menu options for i in range(len(options)): print("{:d}. {:s}".format(i+1, options[i])) # Get a valid menu choice choice = 0 while not(np.any(choice == np.arange(len(options))+1)): choice = inputNumber("Please choose a menu item: ") return choice
def displayMenu(options): # DISPLAYMENU Displays a menu of options, prompts the user to choose an # item and returns the number of the menu item chosen. # # Usage: choice = displayMenu(options) # # Input: options Menu options (cell array of strings) # Output: choice Chosen option (integer) # # Author: From "Creating an interactive menu" made by Mikkel N. Schmidt. # Display menu options for i in range(len(options)): print("{:d}. {:s}".format(i + 1, options[i])) # Get a valid menu choice choice = 0 while not (np.any(choice == np.arange(len(options)) + 1)): choice = inputNumber( "Please choose a menu item by typing its number: ") return choice
def squareRoot(num): if num <= 0: print("Number must be a real integer") return None n = None # WILD GUESS APPROX VALUE OF n FOR THE ROOT OF num # THEN COMPUTE RECURSIVELY ni+1 = (1/2)(ni + (num/ni)) _n = num / 3 #this doesn't matter while True: n = _n _n = (n + (num / n)) / 2 # print(_n) if n == _n: break return n ## USER INPUT print("\n=============================") num = inputNumber("Enter Number(default is 9): ", default=9) ## OUTPUT output = squareRoot(num) if not output: quit() print(f"The square root of number '{num}' is {output}") print("=============================\n")
from inputNumber import inputNumber import math def primeFactors(num): factors = [] i = 2 if num < 2: while num < 2: print("Invalid Input. Enter number equal or more than 2\n") num = inputNumber("Enter Number(default is 10): ", default=10) while i <= num: if (num % i) == 0: factors.append(i) num = num / i else: i += 1 return factors ## USER INPUT print("\n=============================") num = inputNumber("Enter Number(default is 10): ", default=10) ## OUTPUT output = primeFactors(num) print(f"\nPrime factors of number '{num}' is/are: {output} ") print("=============================\n")
from inputNumber import inputNumber import math def output(num): quotient = math.floor(num / 2) last_remainder = num % 2 binary = [str(last_remainder)] while quotient > 0: modulo = quotient % 2 binary.insert(0, str(modulo)) quotient = math.floor(quotient / 2) return "".join(binary) if __name__ == '__main__': ## USER INPUT print("\n=============================") num = inputNumber("Enter Number(default is 50) : ", default=50) ## OUTPUT print(f"\nThe binary form of '{num}' is: {output(num)} ") print("=============================\n")
from inputNumber import inputNumber def output_1(num1, num2): x = num1 y = num2 output = [x, y] while len(output) < 10: last_index = len(output)-1 last_num = output[last_index] prev_num = output[last_index - 1] next_num = prev_num + last_num output.append(next_num) return output if __name__ == '__main__': defaults = [0, 1] ## QUESTION 1 INPUT: print("\n=============================") print("QUESTION 2 INPUT") num1 = inputNumber(f"Enter First Number(default: {defaults[0]}) : ", defaults[0]) num2 = inputNumber(f"Enter Second Number(default: {defaults[1]}) : ", defaults[1]) ## OUTPUT: print(output_1(num1, num2), "\n") print("=============================\n")