简体   繁体   English

您是否可以将if语句分配为变量,以及在计算时如何打印if语句的结果

[英]Can you assign an if statement as a variable and how do you print the result from an if statement when it is a calculation

Hello everyone I am stuck on my homework for python and I have searched the forum and have tried to figure it out for three hours now. 大家好,我停留在我的功课Python和我已经搜查了论坛,并试图现在弄明白了三个小时。 I still can't find an answer or figure it out. 我仍然找不到答案或无法解决。 It might be there but my lack of knowledge may have kept me from seeing it. 可能在那里,但是我缺乏知识可能使我无法看到它。

I'm using python version 3.2.3. 我正在使用python版本3.2.3。

I cannot figure out how to display the results from the if statement in my print statement. 我无法弄清楚如何在我的打印语句中显示if语句的结果。 So if the persons age is < 40 it says "you are young" and if the persons age is > 40 it prints "you look great". 因此,如果年龄小于40岁,则显示“您很年轻”;如果年龄大于40岁,则显示“您看起来很棒”。 I easily have it print the first time but then I need it to print at the end where msg is located I cannot figure out how. 我很容易把它打印的第一次,但后来我需要它在其中msg位于我无法弄清楚如何结束打印。 I included all the code in case I am not explaining this right. 如果没有解释此权限,我会提供所有代码。

print("Hello", firstname, lastname, "you're", age, "years old", msg)

Thanks for the help in advance and again I apologize if the answer is already here somewhere 预先感谢您的帮助,如果答案已经在某个地方,我再次表示歉意

# ITP 100 Python Programming
# In Class Challenge September 10, 2012

# Getting users name and age as input
firstname = input("Hello. What is your first name? ")
lastname = input("What is your last name? ")

birthyear = input("What year where you born? ")
birthyear = int(birthyear)

age = 2012 - birthyear
print("\nYou're", age, "years old.")

if age < 40: 
    print("you are young.")

else:
    print("You look great.")


print("Hello", firstname, lastname, "you're", age, "years old", msg) 


input("\n\nPress n to exit")

You can create a variable that stores a string (your statement either "you are young." or "You look great." , or any string you want!) and display it later in your script. 您可以创建一个存储字符串的变量(您的语句"you are young.""You look great."或任何您想要的字符串!),然后在脚本中显示它。 You have already assigned strings to variables earlier in your script when you create firstname and lastname . 创建firstnamelastname时,已经在脚本的较早部分将字符串分配给了变量。

The problem is, you haven't initialized the variable msg. 问题是,您尚未初始化变量msg。 You can't use it yet, because until the print statement, it hasn't shown up in the program. 您还不能使用它,因为直到print语句,它都没有出现在程序中。 What you should do is store the "You are young" etc. messages in a variable, such as msg. 您应该做的是将“ You are young”等消息存储在变量中,例如msg。 That way you can access it later. 这样,您以后可以访问它。

What about that ? 那个怎么样 ?

if age < 40: 
    msg = "you are young."
else:
    msg = "You look great."
print(msg)

Now, you have defined msg and you can use it later in your final print . 现在,您已经定义了msg ,以后可以在最终print使用它。 And now, for an exercise: find a way to do that automatically when the user enters an age: you would define a function that takes a integer birthyear as input and would output a given message depending on the age... 现在,作为一个练习:找到一种在用户输入年龄时自动执行此操作的方法:您将定义一个函数,该函数将整数birthyear作为输入并根据年龄输出给定的消息...

您可以执行以下操作:

print("You " + "are young." if age < 40 else "look great.")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM