鱼C论坛

 找回密码
 立即注册
查看: 1024|回复: 2

为什么python会报错

[复制链接]
发表于 2021-12-5 12:53:21 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
import random
temp=input("请输入算法:")
small=input("请输入小范围:")
big=input("请输入大范围:")
fy=input("次数:")
frequency=int(fy)
x=1
while x<=frequency:
    one=random.randint(small,big)
    two=random.randint(small,big)
    if temp is '+':
        outcome=input(one,"+",two,"=")
        if one+two==outcome:
            print("OK")
        else:
            print("No")
    else:
        if temp is '-':
           outcome=input(one,"*",two,"=")
           if one-two==outcome:
               print("OK")
           else:
               print("No")     
        else:
           if temp is '*':
               outcome=input(one,"*",two,"=")
               if one*two==outcome:
                    print("OK")
               else:
                    print("No")
           else:
                if temp is '/':
                    outcome=input(one,"/",two,"=")
                    if one/two==outcome:
                        print("OK")
                    else:
                        print("No")
                else:
                    if temp is '%':
                        outcome=input(one,"%",two,"=")
                        if one%two==outcome:
                            print("OK")
                        else:
                            print("No")
                    else:
                        print("输入不对!")
                        end while
    x=x+1
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2021-12-5 19:23:09 | 显示全部楼层
去学一下input内置函数的使用:https://www.runoob.com/python/python-func-input.html
另外end while是什么东西,python没有这样的语法。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-12-5 22:17:27 | 显示全部楼层
本帖最后由 干虾皮 于 2021-12-5 22:47 编辑

C语言有end while,Python没有,其次你的input函数有问题,Python单纯的input应该只有一个参数,您搞了4个参数,如果四个参数的你需要使用input.split(),我根据您的程序猜测您不需要四个参数我给您更改了下,你看下是否符合你想要的结果,对于加,减,乘都试验过了,除和余数应该会有问题涉及(类型:浮点数和整数),您自己搞一下就好,因为你是从C转过的我感觉很可能您的除象要的是地板除,但是我不确认给您两个建议:
1.你的程序能写个说明这样的话别人能看的出来距离你的目的有多大差距。
2.把随机数打印一下或者有提示这样调试的时候会事半功倍

  1. import random
  2. temp=input("请输入算法:")
  3. small=int(input("请输入小范围:"))

  4. big=int(input("请输入大范围:"))
  5. fy=input("次数:")
  6. frequency=int(fy)
  7. x=1
  8. while x<=frequency:
  9.     one=random.randint(small,big)
  10.     two=random.randint(small,big)
  11.     print(one)
  12.     print(two)
  13.     if temp is '+':
  14.         outcome=int(input('one+two='))
  15.         
  16.         if one+two==outcome:
  17.             print("OK")
  18.         else:
  19.             print("No")
  20.     else:
  21.         if temp is '-':
  22.            outcome=int(input("one-two="))
  23.            if one-two==outcome:
  24.                print("OK")
  25.            else:
  26.                print("No")     
  27.         else:
  28.            if temp is '*':
  29.                outcome=int(input("one*two="))
  30.                if one*two==outcome:
  31.                     print("OK")
  32.                else:
  33.                     print("No")
  34.            else:
  35.                 if temp is '/':
  36.                     outcome=float(input("one/two="))
  37.                     if one/two==outcome:
  38.                         print("OK")
  39.                         print
  40.                     else:
  41.                         print("No")
  42.                 else:
  43.                     if temp is '%':
  44.                         outcome=int(input("one%two="))
  45.                         if one%two==float(outcome):
  46.                             print("OK")
  47.                         else:
  48.                             print("No")
  49.                     else:
  50.                         print("输入不对!")
  51.                         
  52.     x+=1
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-4-30 18:59

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表