python帮助获取数据,保存日期,数组,random.shuffle [英] python help with taking in data, saving date, arrays, random.shuffle

查看:70
本文介绍了python帮助获取数据,保存日期,数组,random.shuffle的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是comp科学的初学者,而且我的教授正在使用python,这是全新的汤姆我对程序即时写作有一些问题,我有一些问题。


简要说明

i必须做一个项目,其中有九个盒子填充数字1-8,并且1个空格是空的,游戏需要是加扰,所有的动作都需要保存。游戏可以在游戏中随时暂停。对象是将数字移动到黑点,直到它们全部有序。


网格我需要包含变量,以便玩家可以移动它们


这就是我拥有的东西,我知道它很遥远


def board():

board = [1,2 ,3,4,5,6,7,8,9]


范围内的x(0,9):

print" |" ;


if(board [x] == 9):

print" _"

else:

打印板[x]



if(x == 8):

print" |

board()




当我运行它看起来像这样


|

1

|

2

|

3

|

4

|

5

|

6

|

7

|

8

|

_

|


这就是我需要的样子:


| 1 | 2 | 3 |

| 4 | 5 | 6 |

| 7 | 8 | _ |


我做错了什么?另外我如何使用random.shuffle()函数,这样我可以加扰数字?


帮助会很大程度上适用...


我也将如何保存每一个动作,随机功能也不起作用,所以我可以加扰数字。


i我可能做错了制作游戏板,因为我需要一切都可以互换,当玩家想要移动并将数字放到空白点时,任何帮助任何人???


提前多多感谢


我也似乎无法导入数组或随机

另外我的项目是附加的,如果有人不明白我需要做什么

解决方案

任何愿意帮忙的人?


人?请帮助,将非常感兴趣


你的问题是,每次你发出一个print语句,Python会自动在最后添加一个换行符。


你想要做的是遍历循环而不是直接打印,尝试将值存储到字符串中,该字符串可以与+运算符连接。


在每次迭代中,您可以尝试检查字符串一定长度,如果它是您想要的特定行的长度,则可以打印它,然后清除字符串以重新开始。 br />

示例字符串连接和长度检查...

< span class =codeLinkonclick =Blur(this,this.parentNode.parentNode,getChildren(this),true);>展开 | 选择 | Wrap | 行号


im a beginner at comp science, and my prof is using python, which is totally new tom me i had a few questions with the program im writing, im having a few problems.

brief summary
i have to do a project where there are nine boxes filled with number 1-8, and 1 space is empty, the game needs to be scrambled, also all the moves need to be saved. the game can be paused anytime during thegame. the object is to move the numbers into the black spot till they are all in order.

the grid i have needs to contain variables so that can be moved by the players

this what i have and i know it is far off

def board():
board = [1,2,3,4,5,6,7,8,9]

for x in range(0, 9):
print "|"

if(board[x]==9):
print "_"
else:
print board[x]



if(x==8):
print "|"
board()




when i run that it looks like this

|
1
|
2
|
3
|
4
|
5
|
6
|
7
|
8
|
_
|


and this is how i need it to look:

|1|2|3|
|4|5|6|
|7|8|_|

what am i doing wrong? also how do i use the random.shuffle() function so i can scramble the numbers?

help would be greatly appriciated...

also how would i save every move, also the random fuction isnt working so i can scramble the numbers.


i am probably doing everything wrong to make a game board, cause i need everything to to be interchanglable when the players wants to move and adjecent number to the blank spot, any help anyone???

thanks alot ahead of time

also i can''t seem to import array or random
in addition my project is attached if anyone didnt understand what i needed to to do

解决方案

anyone willing to help?


anyone? please help, will be greatly appriciated


Your problem is that every time you issue a print statement, Python automatically appends a newline character to the end.

What you''ll want to do is iterate through your loop and instead of printing outright, try storing the values to a string, which can be concatenated with the ''+'' operator.

On each iteration you could try checking the string for a certain length, and if it is the length that you want for a particular line you could print it then, and clear the string to start over with a new line.

example string concatenation and length check...

Expand|Select|Wrap|Line Numbers


这篇关于python帮助获取数据,保存日期,数组,random.shuffle的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆