简体   繁体   English

字符串切片是否?

[英]String Slicing with If?

started a class on Python this semester and I'm having troubles with a current assignment. 这个学期在Python上开设了一个课程,目前的作业遇到了麻烦。 Went through the examples and exercises done in class uptil now and couldn't find anything. 直到现在为止,通过课堂上完成的示例和练习,仍然找不到任何东西。

Q1) Fixed 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]

I want it to be able to print out two statements, one for each outcome. 我希望它能够打印出两个语句,每个结果一个。 If the binary number entered ends with a "1" it would spit out aN with 2 decimals and if the binary number entered ends with a "0" it would spit out aN with 1 decimal. 如果输入的二进制数字以“ 1”结尾,它将以2个小数点吐出aN;如果输入的二进制数字以“ 0”结尾,则将以1个小数点吐出aN。

When it runs, it doesn't do anything after showing the value the user inputs for bN 它运行时,在显示用户为bN输入的值后,它什么也不做

Q2) Is there a better way to find digits after the decimal? Q2)是否有更好的方法来查找小数点后的数字? The slicing only works when it's numbers < 10. 切片仅在数字<10时有效。

edit) Ty to the guy that pointed out the string, I totally forgot that :( 编辑)对那个指出字符串的人,我完全忘记了:(

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]

If anyone could answer the second question though, that would be great. 如果有人可以回答第二个问题,那就太好了。

raw_input() takes the input as string. raw_input()将输入作为字符串。 In your if condition you are compering a string with a integer. 在您的if条件中,您正在编译一个带有整数的字符串。 make your if like this, 让你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]

If you want to print 2 digits after the decimal point, you can follow this approach. 如果要在小数点后打印两位数,则可以采用这种方法。

First, raw_input() takes in the input as a str . 首先, raw_input()将输入作为str

So any comparison of aN[-1] with an int will result in False . 因此,将aN[-1]int任何比较都会导致False ie your code will always run the else block. 即您的代码将始终运行else块。

Further, you need to first locate the index of the decimal point and then calculate whether to display one or two places (or deal with the case in case there is no decimal point) 此外,您需要首先找到小数点的索引,然后计算是显示一个还是两个位置(或者处理没有小数点的情况)

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]

Assuming aN always have at least 1 decimals, this should be working. 假设aN总是至少有1个小数,这应该是可行的。 You can use find() to locate the "." 您可以使用find()找到“”。 for decimals. 小数点。

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]

Example: 例:

>>> 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