日本综合一区二区|亚洲中文天堂综合|日韩欧美自拍一区|男女精品天堂一区|欧美自拍第6页亚洲成人精品一区|亚洲黄色天堂一区二区成人|超碰91偷拍第一页|日韩av夜夜嗨中文字幕|久久蜜综合视频官网|精美人妻一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問(wèn)題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營(yíng)銷(xiāo)解決方案
創(chuàng)新互聯(lián)Python教程:python怎么定義用戶輸入

Python中獲取用戶輸入的方法如下:

專(zhuān)注于為中小企業(yè)提供網(wǎng)站設(shè)計(jì)制作、做網(wǎng)站服務(wù),電腦端+手機(jī)端+微信端的三站合一,更高效的管理,為中小企業(yè)杏花嶺免費(fèi)做網(wǎng)站提供優(yōu)質(zhì)的服務(wù)。我們立足成都,凝聚了一批互聯(lián)網(wǎng)行業(yè)人才,有力地推動(dòng)了千余家企業(yè)的穩(wěn)健成長(zhǎng),幫助中小企業(yè)通過(guò)網(wǎng)站建設(shè)實(shí)現(xiàn)規(guī)模擴(kuò)充和轉(zhuǎn)變。

整數(shù)輸入

#!/usr/bin/python3
usr_ip = input("Enter an integer number: ")

# 需要將輸入的字符串顯式地指定為需要的類(lèi)型

usr_num = int(usr_ip)
sqr_num = usr_num * usr_num
print("Square of entered number is: {}".format(sqr_num))

讓我們給定一個(gè)整數(shù)和字符串來(lái)測(cè)定這個(gè)程序

Python docs - 整數(shù)文本

$ ./user_input_int.py
Enter an integer number: 23
Square of entered number is: 529
$ ./user_input_int.py
Enter an integer number: abc
Traceback (most recent call last):
  File "./user_input_int.py", line 6, in 
    usr_num = int(usr_ip)
ValueError: invalid literal for int() with base 10: 'abc'

浮點(diǎn)數(shù)輸入

#!/usr/bin/python3
usr_ip = input("Enter a floating point number: ")
# 需要將輸入的字符串顯式地指定為我們需要的類(lèi)型
usr_num = float(usr_ip)
sqr_num = usr_num * usr_num
# 限制小數(shù)點(diǎn)位數(shù)
print("Square of entered number is: {0:.2f}".format(sqr_num))

E 科學(xué)計(jì)數(shù)法在需要時(shí)可以使用

Python文檔 - 浮點(diǎn)數(shù)文本

Python文檔 - 浮點(diǎn)數(shù)

$ ./user_input_float.py
Enter a floating point number: 3.232
Square of entered number is: 10.45
$ ./user_input_float.py
Enter a floating point number: 42.7e5
Square of entered number is: 18232900000000.00
$ ./user_input_float.py
Enter a floating point number: abc
Traceback (most recent call last):
  File "./user_input_float.py", line 6, in 
    usr_num = float(usr_ip)
ValueError: could not convert string to float: 'abc'

字符串輸入

#!/usr/bin/python3
usr_name  = input("Hi there! What's your name? ")
usr_color = input("And your favorite color is? ")
print("{}, I like the {} color too".format(usr_name, usr_color))

不像Perl,字符串輸入不需要進(jìn)行類(lèi)型轉(zhuǎn)換和注意換行符

$ ./user_input_str.py
Hi there! What's your name? learnbyexample
And your favorite color is? blue
learnbyexample, I like the blue color too

更多知識(shí)請(qǐng)關(guān)注Python視頻教程。


當(dāng)前題目:創(chuàng)新互聯(lián)Python教程:python怎么定義用戶輸入
URL網(wǎng)址:http://www.dlmjj.cn/article/coghcds.html