我正在寻找专门用于python 3.7的功能。我在字符串中有一个数学表达式:
player1 = input("What is player 1's name ? ")
player2 = input("What is player 2's name ? ")
player1 = player1.title()
player2 = player2.title()
while True:
print(player1 + " What do you choose ? rock / paper / scissors : ")
a = input()
print(player2 + " What do you choose ? rock / paper / scissors : ")
b = input()
if a == "rock" and b == "scissors" :
print(player1, "won !!!")
elif a == "scissors" and b == "rock":
print(player2, "won !!!")
elif a == "paper" and b == "rock":
print(player1, "won !!!")
elif a == "rock" and b == "paper":
print(player2, "won !!!")
elif a == "scissors" and b == "paper":
print(player1, "won !!!")
elif a == "paper" and b == "scissors":
print(player2, "won !!!")
elif a == b:
print("Its a tie :-(")
elif a or b != "rock" or "paper" or "scissors":
print("Wrong input, Try again")
end = input("Do you want to play again ? yes/no ") == "yes"
if input == "yes":
continue
else:
print('''
GAME OVER''')
break
# elif input != "yes" or "no":
# print("Wrong input, Try again. yes or no ?")
python 3.7中是否有任何内置函数可以对其进行评估?
PS:eval()函数不起作用。
答案 0 :(得分:2)
Python中电源操作的符号为**
,而不是^
。因此,请先更改您的字符串。
str_ ="2^10"
print(eval(str_.replace('^', '**'))) # -> 1024
请注意,eval
是一个潜在的非常危险的功能。请勿盲目使用。
(非标准)模块numexpr
是更安全的方法。
import numexpr
print(numexpr.evaluate(str_.replace('^', '**'))) # -> 1024
还要注意,不将"^"
替换为"**"
会导致8
。发生这种情况是因为在Python中,^
是binary XOR