亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

用戶輸入以降序排列輸出

用戶輸入以降序排列輸出

狐的傳說 2021-10-26 17:02:54
用戶輸入,其中輸出將按降序排序。例如,輸入一個數字:你想輸入更多:如果是打印輸入另一個數字如果回答否打破argument并按降序對輸出進行排序。我已經用于raw_input查詢和使用while 循環來運行代碼。但它僅按降序對第一個輸出和最后一個輸出進行排序a = raw_input("Enter a number: ")while True:    b = raw_input("Do you want to input more: ")    c = raw_input("Enter another number:")    if b == 'yes':        print (c)        continue    elif b == 'no':        my_list = [a,c]        my_list.sort(reverse=True)        print(my_list)        break我希望代碼能夠成功運行Enter a number:2Do you want to input more:yesEnter another number:3Do you want to input more:yesEnter another number:4Do you want to input more:no[4,3,2]
查看完整描述

3 回答

?
元芳怎么了

TA貢獻1798條經驗 獲得超7個贊

在Python 3.x 中,使用input()代替raw_input()


創建一個list將用戶輸入附加到其中,以便稍后對其進行排序。


input_List = []

input_List.append(int(input("Enter a number: ")))


while True:

    b = input("Do you want to input more: ")

    if b == 'yes':

        input_List.append(int(input("Enter a number: ")))

    elif b == 'no':

        input_List.sort(reverse=True)

        break


print(input_List)

輸出:


Enter a number: 5

Do you want to input more: yes

Enter a number: 7

Do you want to input more: yes

Enter a number: 90

Do you want to input more: no

[90, 7, 5]


查看完整回答
反對 回復 2021-10-26
?
LEATH

TA貢獻1936條經驗 獲得超7個贊

它應該是這樣的:


a = input("Enter a number: ")

l = [int(a)]

while True:

    b = input("Do you want to input more: ")

    if b == 'yes':

        c = input("Enter another number:")

        l.append(int(c))

    elif b == 'no':

        l.sort(reverse=True)

        print(l)

        break


查看完整回答
反對 回復 2021-10-26
?
慕仙森

TA貢獻1827條經驗 獲得超8個贊

很多邏輯錯誤,還有更微妙的錯誤

  • raw_input 返回一個字符串,除非您轉換為整數,否則您將無法獲得正確的數字排序。

  • 是/否問題邏輯被打破,來得太晚了......

  • 你永遠不會append在你的列表中使用,它總是包含 2 個元素。my_list = [a,c]沒有意義。

我的建議:

# this is for retrocompatibility. Python 3 users will ditch raw_input altogether

try:

    raw_input

except NameError:

    raw_input = input   # for python 3 users :)


# initialize the list with the first element

my_list = [int(raw_input("Enter a number: "))]


while True:

    b = raw_input("Do you want to input more: ")

    if b == 'yes':

        # add one element

        my_list.append(int(raw_input("Enter another number:")))

    elif b == 'no':

        my_list.sort(reverse=True)

        print(my_list)

        break


查看完整回答
反對 回復 2021-10-26
  • 3 回答
  • 0 關注
  • 251 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號