python 打印九九乘法表
输入9,开始打印;0:退出
key_msg = input("please key in Number(0:exit):"),调用乘法表函数
键盘输入的是字符型数据
字符串比较与数字比较是否相等注意区分
# This is a sample Python script. # Press Shift+F10 to execute it or replace it with your code. # Press Double Shift to search everywhere for classes, files, tool windows, actions, and settings. import time # ctrl+shift+enter ,添加功能注释 def print_hi(name): # Use a breakpoint in the code line below to debug your script. print(f'Hi, {name}') # Press Ctrl+F8 to toggle the breakpoint. def by2by(num): """ :param num: """ for aa in range(num): print(aa) while True: if num == 0: break else: print(num) num -= 1 def by2by2(num): """ :param num: """ try: num2 = int(num) for bb in range(1, num2 + 1): yy2 = bb for bb2 in range(1, bb + 1): result2 = yy2 * bb2 print(str(bb2) + "*" + str(yy2) + "=" + str(result2) + " ", end="") # end表示不换行 print() except: print("输入错误,请输入数字") def by_key_in_to_calc(num): """ 计算平方根 :param num:输入数字2 """ try: aa = int(num) # 输入字符转换成整型 print(num + "'s square is:" + str(aa * aa)) except: print("输入错误,请输入数字") # Press the green button in the gutter to run the script. if __name__ == '__main__': print_hi('good day') # by2by(5) # by2by2(9) key_msg = input("please key in Number(0:exit):") a = 1 while True: a = a + 1 # time.sleep(5) if key_msg == '0': # 字符串比较相等,键盘输入 print("0:已退出0exit") break else: print("your keyin is: " + key_msg) by_key_in_to_calc(key_msg) by2by2(key_msg) key_msg = input("please key in Number(0:exit):") # See PyCharm help at https://www.jetbrains.com/help/pycharm/
欢迎讨论,相互学习。
cdtxw@foxmail.com