私信  •  关注

maya

maya 最近回复了
3 年前
回复了 maya 创建的主题 » 在Python中将字符串转换为可调用函数

尝试使用compile方法结合eval或exec

a = compile("""def test(): \n print('success') \n return 'success' """, "", "exec")
eval(a)

test()

提出了一些小建议供参考。

  1. 当您确切知道要循环多少次时,可以使用for循环
  2. 使用 关键字来确定布尔类型数据和==

试试这个:

picture = [
    [0, 0, 0, 1, 0, 0, 0],
    [0, 0, 1, 1, 1, 0, 0],
    [0, 1, 1, 1, 1, 1, 0],
    [1, 1, 1, 1, 1, 1, 1],
    [0, 0, 0, 1, 0, 0, 0],
    [0, 0, 0, 1, 0, 0, 0]
]


for row in picture:
    for col in row:
        print(" " if not col else "*", end="")
    print()
3 年前
回复了 maya 创建的主题 » python程序检查python中类型的方法?

我想你需要重新排列代码,试试这个:

import random

def numbers(N,A,B):
    even_count, odd_count = 0, 0
    for i in range(N):
        n = random.randint(A, B)
        if n % 2 == 0:
            even_count += 1
        else:
            odd_count += 1
    return odd_count, even_count


def main():
    a, b = numbers(5, 1, 100)
    print("Number of Odd values = " + str(a))
    print("Number of Even values = " + str(b))


main()