小项目-python和c++练习

python-猜数字游戏

具体规则以及代码的c++版本就参见文章:https://blog.csdn.net/weixin_44991673/article/details/100811733

python代码如下:

import random

num = random.randint(1,100) # 生成1到100之间的一个随机整数 1<= a <=100
print(num)  # 在真实游戏时这句话要注释掉哦!!

while(1):
    guess_num = int(input('Please enter the number you guesse (integer between 1 and'
                          ' 100, end of carriage return):'))  # input输入为字符串。因此要先转换为整数

    if guess_num >=1 and guess_num<=100:
        break
    else:
        print('please enter a number between 1 and 100!!')

while(1):
    if guess_num == num:
        print('Congratulations, you have guessed the right number:',num)
        break
    if guess_num<num:
        print('The guess is less than the true value!')
        guess_num = int(input('Please enter the number you guesse (integer between 1 and'
                              ' 100, end of carriage return):'))
    if guess_num>num:
        print('The guess is greater than the true value!')
        guess_num = int(input('Please enter the number you guesse (integer between 1 and'
                              ' 100, end of carriage return):'))

结果如下:
运行结果
涉及知识:
随机数的产生:num = random.randint(1,100) # 生成1到100之间的一个随机整数 1<= a <=100。
更详尽的随机数产生参见:https://www.cnblogs.com/yunlongaimeng/p/8639963.html
num = input():num为字符串形式。
break的作用:跳出并且结束循环(注意continue是仅仅跳出本次循环,并不会终止循环)。

c+±猜数字游戏

c++代码:

#include <iostream>
#include <ctime>
#define random(a,b) (rand()%(b-a)+a) //generate a integer between a and b
using namespace std;

int main()
{
    int num = 0;
    int guess_num = 0;
    srand((int)time(0));  // 产生随机种子  把0换成NULL也行
    num = random(1,100);
    cout << num << endl;  //真正游戏时也要去掉哦!
    while(1)
        {
        cout << "Please enter the number you guess (integer between 1 and 100, end of carriage return):" << endl;

        cin >> guess_num;

        //cout << int(guess_num) << endl;
        if(guess_num<=100 and guess_num>=1)
            {
            cout <<"right number"<< endl;
            break;
            }
        else{
            cout << "Please enter the number you guess (integer between 1 and 100, end of carriage return):" << endl;
            cin >> guess_num;
            }
        }
    while(guess_num != num){
        if(guess_num<num){
            cout<<"The guess is less than the true value!"<<endl;
            cout << "Please enter the number you guess :" << endl;
            cin >> guess_num;

        }
            if(guess_num>num){
            cout<<"The guess is greater than the true value!"<<endl;
            cout << "Please enter the number you guess :" << endl;
            cin >> guess_num;

        }
    }

    cout<<"Congratulations, you have guessed the right number:"<<num<<endl;

    /*return 0;*/
}

运行结果:

运行结果

涉及知识:(参见文章[https://www.cnblogs.com/xiaokang01/p/9786751.html#_label0_2]
随机数的产生,伪随机数和随机种子的概念。rand()和srand()的用法
c++中的宏定义

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值