我目前正在開發一個接受用戶輸入的程式,并且根據該用戶輸入,a 字串應該改變。我想知道是否有一種方法可以在收到用戶輸入后更改字串。以下是示例代碼。
title = input('Title: ')
subtitle = input('Subtitle: ')
chapter = input('Chapter: ')
subchapter input('Subchapter: ')
title1 = '/title{}'.format(title)
subtitle1 = '/subtitle{}'.format(subtitle)
chapter1 = '/chapter{}'.format(chapter)
subchapter1 = '/subchapter{}'.format(subchapter)
output_txt = '**whatever follows depending on user input**'
print(output_txt)
- 用戶輸入:應該是一個數字
- 然后輸入將被格式化為其透視字串
- 根據用戶輸入的字串output_txt,應該相應地格式化
場景 1: 用戶輸入
Title: 4
Subtitle:
Chapter: 12
Subchapter: 1
output_txt 應該是
output_txt = '/title4/chapter12/subchapter1'
場景 2: 用戶輸入
Title: 9
Subtitle:
Chapter: 2
Subchapter:
output_txt 應該是
output_txt = '/title9/chapter2'
我一直在使用 if elif 但由于可能有多種組合,我認為這樣做不是最有效的。
非常感謝任何正確方向的幫助或提示
uj5u.com熱心網友回復:
您可以在將字串值分配給變數時使用 if-else 條件
title = input('Title: ')
subtitle = input('Subtitle: ')
chapter = input('Chapter: ')
subchapter = input('Subchapter: ')
title1 = '/title{}'.format(title) if title else ''
subtitle1 = '/subtitle{}'.format(subtitle) if subtitle else ''
chapter1 = '/chapter{}'.format(chapter) if chapter else ''
subchapter1 = '/subchapter{}'.format(subchapter) if subchapter else ''
output_txt = title1 subtitle1 chapter1 subchapter1
print(output_txt)
uj5u.com熱心網友回復:
讓我給你介紹一下打字機
typer 將幫助您輕松地使用 python 創建 CLI
因為您的代碼可以這樣處理
import typer
# By defining the data type, we can set the input only a number
def main(title: int = None, subtitle: int = None, chapter: int = None, subchapter: int = None):
title = f'/title{title}' if title else ''
subtitle = f'/subtitle{subtitle}' if subtitle else ''
chapter = f'/chapter{chapter}' if chapter else ''
subchapter = f'/subchapter{subchapter}' if subchapter else ''
output_txt = title subtitle chapter subchapter
print(output_txt)
if __name__ == "__main__":
typer.run(main)
你只需要通過為每個你需要的引數添加引數來運行它
python script_name.py --title 5 --chapter 2 --subchapter 7
uj5u.com熱心網友回復:
只是為了完整性,您可能還想測驗一個數字。
# Test
def test_for_int(testcase):
try:
int(testcase)
return True
except ValueError: # Strings
return False
except TypeError: # None
return False
# Get the inputs
title = input('Title: ')
subtitle = input('Subtitle: ')
chapter = input('Chapter: ')
subchapter = input('Subchapter: ')
# Run the tests and build the string
output_txt = ''
if test_for_int(title):
output_txt = '/title{}'.format(title)
if test_for_int(subtitle):
output_txt = '/subtitle{}'.format(subtitle)
if test_for_int(chapter):
output_txt = '/chapter{}'.format(chapter)
if test_for_int(subchapter):
output_txt = '/subchapter{}'.format(subchapter)
print(output_txt)
uj5u.com熱心網友回復:
你可以做這樣的事情......
def get_user_input(message):
user_input = input(message ' : ')
if user_input == '' or user_input.isdigit() == True:
return user_input
else:
return False
def f():
title = ''
while True:
title = get_user_input('title')
if title == False:
continue
else:
break
subtitle = ''
while True:
subtitle = get_user_input('subtitle')
if subtitle == False:
continue
else:
break
chapter = ''
while True:
chapter = get_user_input('chapter')
if chapter == False:
continue
else:
break
subchapter = ''
while True:
subchapter = get_user_input('subchapter')
if subchapter == False:
continue
else:
break
s = ''
s = '/title' str(title) if title != '' else ''
s = '/subtitle' str(subtitle) if subtitle != '' else ''
s = '/chapter' str(chapter) if chapter != '' else ''
s = '/subchapter' str(subchapter) if subchapter != '' else ''
return s
輸出...
title : 1
subtitle : 2
chapter : 3
subchapter : 4
'/title1/subtitle2/chapter3/subchapter4'
title : 1
subtitle :
chapter : 3
subchapter :
'/title1/chapter3'
顯然,您需要為您的特定目的對代碼添加一些更改。
uj5u.com熱心網友回復:
這是一種使用正則運算式進行輸入驗證和串列推導來收集輸入并構建輸出字串的方法。
import re
def get_input( label: str = None ) -> int:
entry = input(label ': ')
if re.match(r'^\d $', entry) is None:
return None
if int(entry) <= 0:
return None
return int(entry)
tpl_labels = ('Title', 'Subtitle', 'Chapter', 'Subchapter')
lst_values = [get_input(x) for x in tpl_labels]
output_txt = '/'.join([f'{x.lower()}{y}' for x, y in zip(tpl_labels, lst_values) if y])
if not output_txt:
print('No valid responses given.')
else:
output_txt = '/' output_txt
print(output_txt)
get_input() 函式期望用戶輸入的每個值都是一個正整數。所有其他輸入都被靜默忽略(并None回傳)。
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/476251.html
標籤:Python python-3.x 细绳 用户输入
