【问题标题】:How do I get a variable input to print?如何获取要打印的变量输入?
【发布时间】:2018-08-22 00:36:58
【问题描述】:

我正在尝试获取要打印的课堂作业输入。

# Program Name: BadDate.py 
# Function:     This program determines if a date entered by the user is valid.  
# Input:        Interactive
# Output:       Valid date is printed or user is alerted that an invalid date was entered.

validDate = True
MIN_YEAR = 0
MIN_MONTH = 1
MAX_MONTH = 12
MIN_DAY = 1
MAX_DAY = 31

month = float (input("Enter Month "))
day = float (input("Enter Day "))
year = float (input("Enter Year "))


# Get the month, then the day, then the year
# housekeeping()

# Check to be sure date is valid

if int(year) <= MIN_YEAR: # invalid year
    validDate = False
elif int(month) < MIN_MONTH or int(month) > MAX_MONTH: # invalid month
    validDate = False
elif int(day) < MIN_DAY or int(day) > MAX_DAY: # invalid day
    validDate = False

# Test to see if date is valid and output date and whether it is valid or not
if validDate == True:
    print(str(month)+'/'+str(day)+'/'+str(year) " is a valid date") 
else:
    print(str(month)+'/'+str(day)+'/'+str(year) " is an invalid date")

# endOfJob()

当它到达日期时,它给了我一个语法错误。我需要这些语句来打印用户输入的日期。谢谢你。

【问题讨论】:

  • 您在str(year)" is a ...date" 之间缺少+ 符号。应该是str(year) + " is a ...date"

标签: python-3.x input printing


【解决方案1】:

如果您仔细查看给出的错误,您可能会发现它。 现在,作为一个新用户,解码这些错误以及它们来自何处似乎具有挑战性。包含 SyntaxError 堆栈跟踪对您很有帮助,因此我们可以帮助您更快地向您展示问题所在。

无论如何,您只是在调用 print 函数时缺少 2 个简单的连接/加号运算符 (+),所以这里是固定代码:

# Program Name: BadDate.py 
# Function:     This program determines if a date entered by the user is valid.  
# Input:        Interactive
# Output:       Valid date is printed or user is alerted that an invalid date was entered.

validDate = True
MIN_YEAR = 0
MIN_MONTH = 1
MAX_MONTH = 12
MIN_DAY = 1
MAX_DAY = 31

month = float (input("Enter Month "))
day = float (input("Enter Day "))
year = float (input("Enter Year "))


# Get the month, then the day, then the year
# housekeeping()

# Check to be sure date is valid

if int(year) <= MIN_YEAR: # invalid year
    validDate = False
elif int(month) < MIN_MONTH or int(month) > MAX_MONTH: # invalid month
    validDate = False
elif int(day) < MIN_DAY or int(day) > MAX_DAY: # invalid day
    validDate = False

# Test to see if date is valid and output date and whether it is valid or not
if validDate == True:
    print(str(month)+'/'+str(day)+'/'+str(year) + " is a valid date") 
else:
    print(str(month)+'/'+str(day)+'/'+str(year) + " is an invalid date")

# endOfJob()

【讨论】:

    【解决方案2】:

    您可以将所有这些组合成一个条件:

    month = int(input("Enter Month "))
    day = int (input("Enter Day "))
    year = int (input("Enter Year "))
    if 1<=day<=31 and year>0 and 1<=month<=12:
        print(str(month)+'/'+str(day)+'/'+str(year)+ " is a valid date") 
    else:
        print(str(month)+'/'+str(day)+'/'+str(year) +" is an invalid date")
    

    虽然如给定,但这意味着将Feb 30 02 之类的内容视为有效等

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-10-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多