相关疑难解决方法(0)

如何查看运行我的脚本的Python版本?

如何检查Python解释器的哪个版本正在解释我的脚本?

python version

1099
推荐指数
11
解决办法
142万
查看次数

如何格式化小数以始终显示2位小数?

我想要显示:

4949.00

和:

54.954.90

无论小数的长度或是否有任何小数位,我想显示Decimal2位小数,我想以有效的方式做到这一点.目的是显示货币价值.

例如, 4898489.00

python string-formatting

214
推荐指数
9
解决办法
40万
查看次数

在Python中将数字格式化为字符串

我需要找出如何将数字格式化为字符串.我的代码在这里:

return str(hours)+":"+str(minutes)+":"+str(seconds)+" "+ampm
Run Code Online (Sandbox Code Playgroud)

小时和分钟是整数,秒是浮点数.str()函数将所有这些数字转换为十分之一(0.1)的位置.因此,不是我的字符串输出"5:30:59.07 pm",它将显示类似"5.0:30.0:59.1 pm"的内容.

最重要的是,我需要为我做什么库/功能?

python string-formatting

108
推荐指数
4
解决办法
33万
查看次数

有没有办法在Python中的多行字符串中使用变量?

所以我将此作为邮件发送脚本的一部分:

try:
    content = ("""From: Fromname <fromemail>
    To: Toname <toemail>
    MIME-Version: 1.0
    Content-type: text/html
    Subject: test

    This is an e-mail message to be sent in HTML format

    <b>This is HTML message.</b>
    <h1>This is headline.</h1>
    """)
Run Code Online (Sandbox Code Playgroud)

...

    mail.sendmail('from', 'to', content)
Run Code Online (Sandbox Code Playgroud)

我想每次都使用不同的主题(让我们说它是函数参数).

我知道有几种方法可以做到这一点.

但是,我也使用ProbLog来处理我的一些其他脚本(一种基于Prolog语法的概率编程语言).据我所知,在Python中使用ProbLog的唯一方法是通过字符串,如果字符串在几个部分中断了; example =("""string""",variable,"""string2"""),以及上面的电子邮件示例中,我无法使其工作.

我实际上有一些脚本,在多行字符串中使用变量可能很有用,但你明白了.

有没有办法让这项工作?提前致谢!

python string string-interpolation

3
推荐指数
1
解决办法
1986
查看次数

0
推荐指数
2
解决办法
710
查看次数

在 Python 中解码数字?

我有解码问题。首先将字符串'44444'编码为'54'. (5 次 4) 现在当我想解码'54'它的空时。(它确实对字母起作用)

该算法将字符串解码'4a3b2c''aaaabbbcc'。现在当我想解码'4a54'它只是给出'aaaa'但正确的解码是'aaaa44444'. 我怎样才能解码这个?

这是代码:

def decode_RLE(x):
    decode = ''
    count = ''
    for i in x:
        
        if i.isdigit():
            #append to count
            count += i
        else i: 
   
            decode += i * int(count)
            count = '' 
         
    return decode
Run Code Online (Sandbox Code Playgroud)

python algorithm python-3.x rle

0
推荐指数
1
解决办法
113
查看次数

我是编程新手,这段代码中有一个我不明白的错误

该错误显示:“TypeError:第 39 行上的 Add 的操作数类型不受支持:'int' 和 'str'”。这是什么意思以及如何解决这个问题?这是代码:

import time
TimeIsUp=0
print ("Timer")
h=int(input("Hours-"))
m=int(input("Minutes-"))
if m>59:
 while m>0 and m!=0:
  m-=60
  h+=1
 m+=60
 h-=1
s=int(input("Seconds-"))
if s>59:
 while s>0 and s!=0:
  s-=60
  m+=1
 s+=60
 m-=1
 while m>=0 and m!=0:
  m-=60
  h+=1
 m+=60
 h-=1
while TimeIsUp==0:
 s-=1
 if s<0 and m>0:
  s+=61
  m-=1
  if m<0 and h>0:
   m+=61
   h-=1
 else:
  if h>0:
   s+=61
   m+=59
   h-=1
  else:
   TimeIsUp==1
 print (h+":"+m+":"+s)
 time.sleep(1)
print ("Time's Up!")
Run Code Online (Sandbox Code Playgroud)

“时间”从https://trinket.io/python导入(因为这是我作为初学者编写 Phython 时使用的)。

python time

-2
推荐指数
1
解决办法
118
查看次数