小编use*_*923的帖子

Python:如何在错误输入之前重复最后一个输入提示?

我有一个for语句,提示用户输入5.数字.像这样:

"Input 1. number:

input 2. number:

..
..
.."
Run Code Online (Sandbox Code Playgroud)

我想重复用户在输入错误之前得到的最后一个提示(数字太大).但我的程序跳过了错误的程序:

像这样

"Input 1. number:
5
Accepted

input 2. number:
999
Wrong! Retry
(here I use *continue* for the loop)

input 3.number:

---"
Run Code Online (Sandbox Code Playgroud)

我该怎么做才能重新提出第二个问题?

python loops for-loop while-loop

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

__repr __()返回非字符串

所以我有一个类方法,我想用它来绘制字典及其值:

 def __repr__ (self): 
        for row in zip(*([ky]+map(str,val) for ky,val in (self.slovar.items()))):

           print"\t".join (row)
Run Code Online (Sandbox Code Playgroud)

如果是这样的话,我会得到一个理想的输出:

>>> test
n   n1  n2
1   a   2.3
2   b   2.1
3   d   2.5

Traceback (most recent call last):
  File "<pyshell#521>", line 1, in <module>
    test
TypeError: __repr__ returned non-string (type NoneType)
Run Code Online (Sandbox Code Playgroud)

但也是一个Traceback错误.

如果我返回值而不是打印出来,我只能得到这个:

>>> test
n   n1  n2
Run Code Online (Sandbox Code Playgroud)

如果我制作一个自定义方法而不是默认的'系统',它可以正常工作...(我需要它是默认的)

python string repr traceback

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

从随机的ascii字母和数字制作一个列表

我想从随机数字和字母生成一个列表.我成功了:

def rand_num():

    while True:
        random_char= random.choice(string.ascii_letters+string.digits)

        random_lst= [random_char]
Run Code Online (Sandbox Code Playgroud)

但是当我想要打印这个列表时,print random_lst我会得到如下输出:

['W']
['i']
['P']
['6']
['P']
['B']
['d']
['f']
['n']
['j']
Run Code Online (Sandbox Code Playgroud)

代替:

['W','i','P'.... 等等

我该怎么办?在这种情况下,.choice是错误的功能吗?

python string random list choice

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

标签 统计

python ×3

string ×2

choice ×1

for-loop ×1

list ×1

loops ×1

random ×1

repr ×1

traceback ×1

while-loop ×1