python猜数字

这个是别人问我的,代码报错了,原来长这样

girl=3
count=0
while count<3:
    a=input("how old is girl:")
    if a<girl:
        print("猜小了")
    elif a>girl:
        print("猜大了")
    else:
        print("算你聪明")
    break
count+=1
if count>=3:
    b=input("是否继续:")
    if b in("是,yes,Yes"):
        count=o
    else:
        print("再见")



解决方案

1、python写的

girl=3

count=0

while count<3:

    count+=1#把count++放到循环里面

    a=int(input("how old is girl:"))#input获得的是str类型,不能直接和int比较,在前面加int()

    if a<girl:#输入的数小于3

        print("猜小了")

    elif a>girl:#输入的数大于3

        print("猜大了")            

    else:

        print("算你聪明")

        break

    if count>=3:#同↑

        b=input("是否继续:")

        if b in ['是','yes','Yes']:#in、not in检查特定值是否在列表中,是列表,你之前用的是字符串,也能运行,但是相当于b只要等于“是、y、e、s、Y、,”中任意组成的东西,等式都成立

            count=0        

        else:

            print("再见")

2、用C++改写(非本人写)

#include <iostream>
#include <cstring>
using namespace std;
int main()
{
    int girl = 3;
    int a;
    cout << "how old the girl" << endl;
A:
    for (int i = 0;i < 3;i++)
    {
        cin >> a;
        if (a < girl)
        {
            cout << "猜小了" << endl;
        }
        else if (a > girl)
        {
            cout << "猜大了" << endl;
        }
        else
        {
            cout << "算你聪明" << endl;
            return 0;
        }
    }
    cout << "是否继续:" << endl;
    char n[100];
    cin >> n;
    if (getchar() == 'y' || !((strcmp(n, "yes"))) || !((strcmp(n, "YES"))) || !((strcmp(n, "是"))))
    {

        goto A;
    }
    else
        cout << "再见" << endl;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值