-8

只是为了好玩,我正在尝试将下面编写的 python 计算器程序转换为 PHP。

谁能帮帮我/这可能吗?

该程序在下面,我想通过 PHP 获得相同的输出。

#!/usr/bin/env python
#
#title: calculator.py
#author: Samuel Peppard
#purpose:
#

# 1 equates a loop, everything else does not.
loop = 1 

# Contains the player's choice
choice = 0 

while loop == 1:

# Print what options you have
print " "
print "Basic Calculator Program!"

print " "
print "Select your function by typing its designated number..."

print " "
print "1) Addition"
print "2) Subtraction"
print "3) Multiplication"
print "4) Division"
print "5) Quit"
print " "

# allows for raw input to be read
choice = int(raw_input("Choose your option: ").strip())

if choice == 1:
    add1 = input("Add this amount: ")
    add2 = input("to this amount: ")
    print " "
    print add1, "+", add2, "=", add1 + add2

elif choice == 2:
    sub2 = input("Subtract this amount: ")
    sub1 = input("from this amount: ")
    print " "
    print sub1, "-", sub2, "=", sub1 - sub2

elif choice == 3:
    mul1 = input("Multiply this amount: ")
    mul2 = input("with this amount: ")
    print " "
    print mul1, "*", mul2, "=", mul1 * mul2

elif choice == 4:
    div1 = input("Divide this amount: ")
    div2 = input("by this amount: ")
    print " "
    print div1, "/", div2, "=", div1 / div2

elif choice == 5:
    loop = 0

print "Goodbye for now!"
4

1 回答 1

2

回答你的第二个问题:

这可能吗?

当然是的。PHP 和 python 都是图灵完备的,都可以接受控制台输入。

于 2012-11-09T21:42:01.183 回答