无法执行此程序.错误:语法 random.py 无效


import random
print "n wlecome to fortune game"

your_fortune = random.randrange(1,8)
if your_fortune == 1:
    print "your are lucky"
else:
    print "you are not lucky"

raw_input("按回车键退出")

我认为Ashwini的版本已经完成了这项工作。当我看你的原始代码时,它是这样说的:

import random
print "n wlecome to fortune game"
your_fortune = random.randrange(1,8)
if your_fortune == 1: print "your are lucky" else: print "you are not lucky"
raw_input("press enter to exit")

在第四行,我在同一行看到 if 和 else 语句。这是一种内在的语法。使用这个:

import random
print "nWelcome to fortune game"
your_fortune = random.randrange(1,8)
if your_fortune == 1: print "your are lucky"
else: print "you are not lucky"
raw_input("press enter to exit")

或者这个:

import random
print "nWelcome to fortune game"
your_fortune = random.randrange(1,8)
p = "your are lucky" if your_fortune == 1 else "you are not lucky"
print p
raw_input("press enter to exit")

最新更新