簡體   English   中英

字符串切片是否?

[英]String Slicing with If?

這個學期在Python上開設了一個課程,目前的作業遇到了麻煩。 直到現在為止,通過課堂上完成的示例和練習,仍然找不到任何東西。

Q1)固定

aN = raw_input("Enter a number with decimals")
bN = raw_input("Enter a binary number, 2 to 4 digits in length")

if (bN[-1] == 1):
    print "The binary number was odd so your number will contain 2 decimals"
    print "The number is now",aN[0:4],

elif (bN[-1] == 0):
    print "The binary number was even so your number will contain 1 decimal"
    print "The number is now",bN[0:3]

我希望它能夠打印出兩個語句,每個結果一個。 如果輸入的二進制數字以“ 1”結尾,它將以2個小數點吐出aN;如果輸入的二進制數字以“ 0”結尾,則將以1個小數點吐出aN。

它運行時,在顯示用戶為bN輸入的值后,它什么也不做

Q2)是否有更好的方法來查找小數點后的數字? 切片僅在數字<10時有效。

編輯)對那個指出字符串的人,我完全忘記了:(

aN = raw_input("Enter a number with decimals")
bN = raw_input("Enter a binary number, 2 to 4 digits in length")

if (float(bN[-1]) == 1):
    print "The binary number was odd so your number will contain 2 decimals"
    print "The number is now",aN[0:4],

elif (float(bN[-1]) == 0):
    print "The binary number was even so your number will contain 1 decimal"
    print "The number is now",aN[0:3]

如果有人可以回答第二個問題,那就太好了。

raw_input()將輸入作為字符串。 在您的if條件中,您正在編譯一個帶有整數的字符串。 讓你if這樣,

if float(bN[-1]) == 1:
    print "The binary number was odd so your number will contain 2 decimals"
    print "The number is now %.2f" % float(aN)

elif float(bN[-1]) == 0:
    print "The binary number was even so your number will contain 1 decimal"
    print "The number is now", bN[0:3]

如果要在小數點后打印兩位數,則可以采用這種方法。

首先, raw_input()將輸入作為str

因此,將aN[-1]int任何比較都會導致False 即您的代碼將始終運行else塊。

此外,您需要首先找到小數點的索引,然后計算是顯示一個還是兩個位置(或者處理沒有小數點的情況)

aN = raw_input("Enter a number with decimals")
bN = raw_input("Enter a binary number, 2 to 4 digits in length")

if (bN[-1] == '1'):
    print "The binary number was odd so your number will contain 2 decimals"
    if aN.find('.') == -1:
        print "The number is now ",aN
    else:
        print "The number is now ", aN[:aN.find('.')+3]

elif (bN[-1] == '0'):
    print "The binary number was even so your number will contain 1 decimal"
    if aN.find('.') == -1:
        print "The number is now ",aN
    else:
        print "The number is now ", aN[:aN.find('.')+2]

假設aN總是至少有1個小數,這應該是可行的。 您可以使用find()找到“”。 小數點。

aN = raw_input("Enter a number with decimals")
bN = raw_input("Enter a binary number, 2 to 4 digits in length")

if (bN[-1] == "1"):
    print "The binary number was odd so your number will contain 2 decimals"
    print "The number is now", aN[:aN.find(".")+3]

elif (bN[-1] == "0"):
    print "The binary number was even so your number will contain 1 decimal"
    print "The number is now", aN[:aN.find(".")+2]

例:

>>> aN
'1.12345'
>>> bN
'11'
>>> if (bN[-1] == "1"):
...     print "The binary number was odd so your number will contain 2 decimals"
...     print "The number is now", aN[:aN.find(".")+3]
... elif (bN[-1] == "0"):
...     print "The binary number was even so your number will contain 1 decimal"
...     print "The number is now", aN[:aN.find(".")+2]
...
The binary number was odd so your number will contain 2 decimals
The number is now 1.12

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM