请给我一个简单的代码示例
时间: 2023-05-27 16:07:43 浏览: 40
以下是一个简单的 Python 代码示例,将两个数字相加并输出结果:
```
num1 = 5
num2 = 7
sum = num1 + num2
print("The sum of", num1, "and", num2, "is", sum)
```
输出结果为:
```
The sum of 5 and 7 is 12
```
相关问题
那请给我一个数字炸弹的代码示例
好的,请看以下的 Python 代码示例:
```python
import random
def game():
secret_number = random.randint(1, 100)
attempts = 0
print("Welcome to the Number Bomb Game!")
print("I have chosen a number between 1 and 100. Can you guess it?")
while True:
guess = int(input("Enter your guess: "))
attempts += 1
if guess < secret_number:
print("Too low! Try a higher number.")
elif guess > secret_number:
print("Too high! Try a lower number.")
else:
print(f"Congratulations! You guessed the number in {attempts} attempts!")
break
game()
```
这个代码示例实现了一个简单的数字炸弹游戏。游戏开始时,会随机生成一个1到100之间的秘密数字。玩家需要通过输入猜测的数字来尝试猜中秘密数字。如果猜测的数字比秘密数字小,程序会提示玩家尝试一个更大的数字;如果猜测的数字比秘密数字大,程序会提示玩家尝试一个更小的数字。直到玩家猜中秘密数字,游戏结束并显示玩家所用的尝试次数。
请注意,这只是一个简单的示例代码,您可以根据自己的需求进行修改和扩展。希望对您有帮助!
给我一个login的代码示例
好的,以下是一个简单的login代码示例:
```python
username = input("请输入用户名:")
password = input("请输入密码:")
if username == "admin" and password == "123456":
print("登录成功!")
else:
print("用户名或密码错误!")
```
这个示例中,用户需要输入用户名和密码,如果输入的用户名和密码与预设的值匹配,则登录成功,否则登录失败。