使用'*'和'O'字符创建以下10 x 5网格。每行必须只有一个'O'字符,'O'字符的位置必须是随机的。 [英] Create the following 10 x 5 grid with ‘*’ and ‘O’ characters. Each row must have exactly one ‘O’ character, and the position of ‘O’ characters must be random.
本文介绍了使用'*'和'O'字符创建以下10 x 5网格。每行必须只有一个'O'字符,'O'字符的位置必须是随机的。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
******* O **
****** O ***
****** O *** < br $>
* O ********
***** O ****
我尝试了什么:
i = 1
m = 0
而i< ; = 5:
j = 1
m = m + 1
而j< = 10:
如果j == m + 7:
print( 'O',end ='')
else:
print('*',end ='')
j = j + 1
print()
i = i + 1
解决方案
好的,所以你必须将基本逻辑降低。你只需要用生成随机数的东西替换m = m + 1行,m = 某事...... 。
您还应该摆脱m + 7的事情并将其设为m。
参见 9.6。 random - 生成伪随机数 - Python 3.4.7文档 [ ^ ]。
*******O**
******O***
******O***
*O********
*****O****
What I have tried:
i=1 m=0 while i<=5: j=1 m=m+1 while j<=10: if j==m+7: print('O',end='') else: print('*',end='') j=j+1 print() i=i+1
解决方案
OK, so you have to basic logic down. You just need to replace the "m=m+1" line with something that generates a random number, "m=something...".
You should also get rid of the "m+7" thing and make that just "m".
See 9.6. random — Generate pseudo-random numbers — Python 3.4.7 documentation[^].
这篇关于使用'*'和'O'字符创建以下10 x 5网格。每行必须只有一个'O'字符,'O'字符的位置必须是随机的。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文