📅  最后修改于: 2023-12-03 15:33:57.764000             🧑  作者: Mango
数字猜谜游戏是一种简单有趣的游戏,适合编程初学者进行练习。Python 3 和 C 都可以实现这个游戏。本文将介绍如何使用 Python 3 和 C 分别实现数字猜谜游戏。
import random
num = random.randint(1, 100)
guess = -1
print("Welcome to the number guessing game!")
while guess != num:
guess = int(input("Please guess a number between 1 and 100: "))
if guess == num:
print("Congratulations, you guessed the number!")
elif guess < num:
print("The number is higher than your guess.")
else:
print("The number is lower than your guess.")
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main() {
int num, guess = -1;
srand(time(NULL));
num = rand() % 100 + 1;
printf("Welcome to the number guessing game!\n");
while (guess != num) {
printf("Please guess a number between 1 and 100: ");
scanf("%d", &guess);
if (guess == num) {
printf("Congratulations, you guessed the number!\n");
} else if (guess < num) {
printf("The number is higher than your guess.\n");
} else {
printf("The number is lower than your guess.\n");
}
}
return 0;
}
Python 3 和 C 都是常见的编程语言,都可以用来实现数字猜谜游戏。Python 3 是一种简单易学的脚本语言,适合初学者练习,而 C 是一种高效的系统语言,适合实现更加复杂和底层的应用。无论使用哪种语言实现数字猜谜游戏,都需要深入理解条件判断和循环语句的使用,并学会调用库函数来实现特定的功能。