我的第一个 Python 代码。
所以,我大约 45 分钟前才开始编码,在尝试自己解决这个问题后,我决定寻求一些帮助。请随时对我的代码草率、不稳定等提出一些建设性的批评。我在 Lua 方面比在 Python 方面要好得多。我在 IDLE Python GUI 中遇到的问题是,当我按 F5 在 Python Shell 中运行它时,它给了我一个错误,说“需要一个缩进块”,但没有告诉我在哪里放置缩进块。我想从一些简单的事情开始,所以我开始编写一个有效的“年龄计算器”,代码如下:
# Importing necessary modules #
import time
import math
# Start Application #
print ("The age calculator!")
time.sleep(1)
print ("May I collect some information about you?")
yn = (input("Yes or No: "))
# if-elif Based Decisions #
if yn != Yes or No:
ynInvalid()
elif yn == No:
exit()
elif yn == Yes:
ynYes()
elif yn != Yes or No:
ynInvalid()
#Defining Functions#
def PrintInfo():
print ("Hello", name, "Nice to meet you!")
def AgeCheck():
if age <= 0:
print("Invalid Age!")
elif age > 0:
days = age * 365
minutes = age * 525948
seconds = age * 31556926
print("Doing some math please wait...")
time.sleep(4)
print(name,"you been alive for", days,"days", minutes, "minutes and", seconds, "seconds, Wow you're really old!")
def ynYes():
time.sleep(2)
print (" Please enter your name below.")
time.sleep(1)
name = input("Name: ")
time.sleep(1)
PrintInfo()
age = int(input("Age: "))
AgeCheck()
def ynYesRetry():
age = int(input("Age: "))
AgeCheck()
def ynInvalid():
print("Invalid Argument! Please try Again!")
ynYesRetry()