当前位置 : 主页 > 编程语言 > python >

python-6.接受用户输入

来源:互联网 收集:自由互联 发布时间:2022-07-07
先来第一个版本: exercise 11 Asking Question print ( "How old are you?" , end = " " ) age = input () print ( "How tall are you?" , end = " " ) height = input () print ( "How much do you weight?" , end = " " ) weight = input () print (

先来第一个版本:

exercise 11 Asking Question

print("How old are you?",end=" ")
age = input()

print("How tall are you?",end=" ")
height = input()

print("How much do you weight?",end=" ")
weight = input()

print(f"So, you're {age} old,{height} tall and {weight} heavy.")

python-6.接受用户输入_使用说明

input函数会接收用户输入,程序里需要把参数保存到变量,否则就没有意义.

exercise 12 Prompting People

age=input("你多大了? ")
height=input("小哥哥今年多高? ")
weight = input("你体重多少? ")

print(f"好了,我现在知道你的年龄是{age}岁,身高是{height}cm,体重是{weight}公斤.")

python-6.接受用户输入_使用说明_02

这样写起来更加清爽,简便,input里面是提示语,终端输入是值.

查看模块帮助,-m参数表示引用的模块,例如下面,我们引用帮助文档模块,插卡input的使用说明.
python-6.接受用户输入_帮助文档_03
再来看一个:
python-6.接受用户输入_使用说明_04

exercise 13 parameters upacking variables

from sys import argv

# read the WYSS section for how to run this
script,first,second,third = argv

print("The script is called:",script)
print("Your first variable is:",first)
print("Ypur secod variable is:",second)
print("Your third variable is:",third)

python-6.接受用户输入_帮助文档_05

一次獲取多個參數,並把參數分配給相應的變量.

exercise 14 Prompting and Passing

from sys import argv

script, user_name = argv
prompt = '> '

print(f"Hi {user_name}, I'm the {script} script.")
print(f"I'd like to ask you a few questions.")
print(f"Do you like me {user_name}?")

likes = input(prompt)

print(f"Where do you like live {user_name}?")
lives = input(prompt)

print(f"What kind of compture do you have?")
compture = input(prompt)

print(f"""
Alright, so you said {likes} about liking me.
You live in {lives}. Not sure where that is.
And you have a {compture} compture.Nice.
""")

python-6.接受用户输入_帮助文档_06


上一篇:python-5.原样输出,转义字符
下一篇:没有了
网友评论