【问题标题】:Convert Python calculator to PHP [closed]将 Python 计算器转换为 PHP [关闭]
【发布时间】:2012-10-30 06:21:24
【问题描述】:

只是为了好玩,我正在尝试将下面编写的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!"

【问题讨论】:

  • 程序缺少pythonic的所有内容,所以转换应该没问题。
  • 唯一棘手的一点可能是你可能需要fgets(STDIN),如果你打算在命令行上使用它,剩下的就是......好吧......作为基本计算器可以...... . 开始编码 ;)
  • 只是为了好玩?您似乎在尝试让其他人为您完成这项工作?
  • 我还没有尝试过任何东西,我只是想在我开始之前知道这是否可行。
  • 这是我无情的要求吗?

标签: php python calculator


【解决方案1】:

回答你的第二个问题:

这可能吗?

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

【讨论】:

猜你喜欢
  • 2011-05-06
  • 2012-12-17
  • 2012-12-03
  • 2011-01-15
  • 2016-04-18
  • 1970-01-01
  • 1970-01-01
  • 2012-10-24
  • 2020-05-06
相关资源
最近更新 更多