我正在尝试将一些旧的qbasic(旧的dos basic)代码转换为python。我知道python,但没有太多的qbasic(除了猜测语法的含义之外)。这是qbasic代码
1020 DIM XS(499), A(504), V(99)
1560 GOSUB 2600 'Get coefficients
2600 REM Get coefficients
2660 CODE$ = "A"
2680 M% = 3
2690 FOR I% = 1 TO M% 'Construct CODE$
2700 GOSUB 2800 'Shuffle random numbers
2710 CODE$ = CODE$ + CHR$(65 + INT(25 * RAN))
2720 NEXT I%
2730 FOR I% = 1 TO M% 'Convert CODE$ to coefficient values
2740 A(I%) = (ASC(MID$(CODE$, I% + 1, 1)) - 77) / 10
2750 NEXT I%
2760 RETURN
2800 REM Shuffle random numbers
2810 IF V(0) = 0 THEN FOR J% = 0 TO 99: V(J%) = RND: NEXT J%
2820 J% = INT(100 * RAN)
2830 RAN = V(J%)
2840 V(J%) = RND
2850 RETURN
似乎正在将ASCII码映射到随机数,但是由于我不熟悉语法J%和V(J%)等(我不知道%的含义),我不清楚如何实现
答案 0 :(得分:1)
如前所述,在QBasic中,%
,$
和#
表示变量的数据类型。在Python中,您不必指定变量的数据类型,但是在QBasic中,就像这样:
QBasic
I% ' % = integer variable
str$ ' $ = string variable
f# ' # = floating point variable
如果要转换QBasic函数,请参见以下问题:Python equivalent of mid和Python get ASCII value
QBasic Python
str$ = "water" str = "water"
ASC("A") ord("A") // result 65
MID$(str$, 3, 2) str[3,(3+2)] // result "te"
CHR$(65) chr(65) // result "A"