Python中循环题目二的练习(for、while、if)

文章目录

一、计算器:

请输入两个数和一个符号,完成两个数的+ - * / %  // ** 
 from secrets import choice
 a=int(input("请输入第一个数字:"))
b=int(input("请输入第二个数字:"))
 c=str(input("请在以下字符中选择一个:+ - * / %  // ** :"))
 while   c:
        if c=="+":
             print("两个数字之和为:%s"%(a+b))
       elif c=="-":
                print("两个数字之和为:%s"%(a-b))
       elif c=="*":
             print("两个数字之和为:%s"%(a*b))
        elif c=="/":
             print("两个数字之和为:%s"%(a/b))
        elif c=="%":
             print("两个数字之和为:%s"%(a%b))
        elif c=="//":
            print("两个数字之和为:%s"%(a//b))
       elif c=="**":
             print("两个数字之和为:%s"%(a**b))
        break

在这里插入图片描述

二、设计一个程序,完成(英雄)商品的购买(界面就是第一天打印的界面)

	展示商品信息(折扣)->输入商品价格->输入购买数量->提示付款
	输入付款金额->打印购买小票(扩展)
 from random import choice
 print("英雄商城首页")
 print("英雄商城英雄列表")
 print('''
                                  英雄商城英雄列表

 ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~

 编号  姓名     昵称    价格   折扣  库存  描述

 1     纳尔   迷失之牙  3500   9.5   100  丛林不会原谅盲目与无知

 2     锐雯   放逐之刃  4000   9.5   100  她是残忍高效的战士  

 3     薇恩   暗夜猎手  3500   9.56  100  这个世界不想人们想象的那么美好

 4     扎克   生化魔人  3000   9.8   100  即使你没有脊柱,你也必须站起来

 5     杰斯  未来守护者 2500   6.5   100  武装着睿智与魅力,你的选择没有错

 ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~
 ''')
 choice==int(input("请您选择商品:"))
 a=int(input("请输入商品价格:"))
 num=int(input("请输入购买数量:"))
 c=float(input("请输入您购买的商品的折扣:"))
 payways=str(input("请您选择以下的付款方式:支付宝、微信、或者是银行卡:"))
 if     payways=="支付宝":
               print("您选择的支付方式为:支付宝,您需要支付的金额为:%s"%(a*num*c/10))
 elif   payways=="微信":
              print("您选择的支付方式为:微信,您需要支付的金额为:%s"%(a*num*c/10))
 elif   payways=="银行卡":
              print("您选择的支付方式为:银行卡,您需要支付的金额为:%s"%(a*num*c/10))
 else:
               print('请输入正确的支付方式:')


在这里插入图片描述

三、健康计划

用户输入身高(m),体重(kg)

计算公式:BMI = 体重 / 身高^2

	BMI < 18.5:过轻
	18.5≤ BMI <24:正常
	24 ≤ BMI <27:过重
	27 ≤ BMI < 30:轻度肥胖
	30 ≤ BMI < 35:中度肥胖
	BMI ≥ 35:重度肥胖
 from tokenize import Double
 print("**********************健康计划**********************")
 a=float(input("请输入您的身高:"))
 b=float(input("请输入您的体重:"))
 BMI = b/(a*a)
 if BMI <18.5:
        print("您的体重过轻")
 elif 18.5<=BMI <24:
        print("您的体重正常")
 elif 24 <= BMI <27:
        print("您的体重过重")
 elif 27 <=BMI <30:
       print("您的体重轻度肥胖")
 elif 30 <= BMI < 35:
        print("您的体重中度肥胖")
 elif BMI >=35:
       print("您的体重重度肥胖")
 else:
       print("请输入正确答案!!")


在这里插入图片描述

四、打印如下图案:

1、
*
**
***
****
*****
******

from operator import index
layer = int(input("请输入您要打印的层数:"))
index = 1
while index <= layer:
	# 打印每一层的*的个数
	j = 1
	while j <= index:
		print("*", end="")
		j += 1
	print()
	# print("*" * index)
	index += 1

在这里插入图片描述

2、
*
***
*****
*******
*********

layer = int(input("请输入您好打印的层数:"))
i = 1
while i <= layer:
	# 求出空格的数量
	a = layer - i
	j = 1
	while j <= a:
		print(" ", end="")
		j += 1
	# 求*的数量
	b = 2 * i - 1
	j = 1
	while j <=b:
		print("*", end="")
		j += 1
	print()
	i += 1

在这里插入图片描述

3、
*********
*******
*****
***
*

layer = int(input("请输入您好打印的层数:"))
for i in range(layer, 0, -1):
	# 求出空格的数量
	a = layer - i
	# 求*的数量
	b = 2 * i - 1
	print(" "*a + "*"*b)

在这里插入图片描述

4、

    *
   ***
  *****
 *******
*********
 *******
  *****
   ***
    *
layer= int(input("请输入您要打印的层数"))
b = layer
c = layer
for i in range(1, layer + 1):   
    print(" " * (b - 1), "*" * (2 * i - 1))
    b -= 1
    if i == layer:  
        for y in range(1, layer):
            print(" " * y, "*" * (2*c-3))
            c -= 1

在这里插入图片描述

5、
*
* *
* *
* *
* *
* *
* *
* *
*

layer = int(input("请输入您要打印的层数:"))
b = layer
c = layer
print(" " * (layer - 1), "*")
for i in range(2, layer+1): 
    print(" " * (b - 1) + "*" + " " * (2 * i - 3) + "*")
    b -= 1
    if i == layer: 
        for y in range(2, layer):
            print(" " * y+"*"+" "*(2*c-5)+ "*" )
            c -= 1
        print(" "*layer+"*")

在这里插入图片描述

6、

    *
   ***
  * * *
 *  * *
*********
 *  *  *
  * * *
   ***
    *
layer= int(input('请输入您要输入的层数: '))
m = t = 2 * layer-1           
while m >= 1:             
    if(m == t or m == 1):  
        print('  ' * layer + '*' + '  ' * 4 * (layer-1))
    elif m == layer:
        print('  '+' *' * (2 * layer - 2))
    elif m > layer:          
        print('  ' * (m - layer + 1) + '*' +'  ' * (t - m - 1) + ' *' +'  ' * (t - m - 1) + ' *')
    else:                
        print('  ' * (layer - m + 1) + '*' + '  ' * ((2 * m - 3) // 2) + ' *' + '  ' * ((2 * m - 3) // 2) + ' *')
    m -= 1

在这里插入图片描述

五、输入数,判断这个数是否是质数

num = int(input("请输入数字:"))
for i in range(2,num):
          if num%i==0:
                print("这个不是质数!!")
                break
else:
       print("这个数是质数!!")

在这里插入图片描述

六、让用户输入一个月份,判断这个月是哪个季节?假定3到4月是春季,5到8月是夏季,9到10是秋季,11、12、1、2月是冬季

from calendar import month
month=int(input("请输入月份:"))
for i in range(1,12):
       if   month==1:
              print("%s"%month+"月"+"是冬季!!")
       elif month==2:
               print("%s"%month+"月"+"是冬季!!")
       elif month==3:
               print("%s"%month+"月"+"是春季!!")
       elif month==4:
               print("%s"%month+"月"+"是春季!!")
       elif month==5:
               print("%s"%month+"月"+"是夏季!!")
       elif month==6:
               print("%s"%month+"月"+"是夏季!!")
       elif month==7:
               print("%s"%month+"月"+"是夏季!!")
       elif month==8:
               print("%s"%month+"月"+"是夏季!!")
       elif month==9:
               print("%s"%month+"月"+"是秋季!!")
       elif month==10:
               print("%s"%month+"月"+"是秋季!!")
       elif month==11:
               print("%s"%month+"月"+"是冬季!!")
       elif month==12:
               print("%s"%month+"月"+"是冬季!!")
       break
if month>12:
       print("请输入正确的月份!!")

在这里插入图片描述

七、提示用户输入用户名,然后再提示输入密码,如果用户名是“admin”并且密码是“88888”,则提示正确,否则,如果用户名不是admin还提示用户用户名不存在,如果用户名是admin则提示密码错误。

username=str(input("请输入用户名:"))
while username=="admin":
      password=int(input("请输入您的密码:"))
      if   password==88888:
             print("您输入的密码是正确的!")
             break
      elif password!=88888:
              print("密码错误")
              break
else:
              print('用户不存在')

在这里插入图片描述

八、有一些四位数,百位数字都是3,十位数字都是6,并且它们既能被2整除,又能被3整除,求这样的四位数中最大的和最小的两数各是几?

for i in range(1000, 10000):
	hundredth = i % 1000 // 100
	ten = i % 100 // 10
	if hundredth == 3 and ten == 6 and i % 6 == 0:
		print(f"{i}")

在这里插入图片描述

九、编程求一个四位自然数ABCD,它乘以A后变成DCBA

a = input("请输入一个四位数:")
while True:
    b = int(a)
    break
num4 = b // 1000
num3 = b // 100 - b // 1000 * 10
num2 = b // 10 -  b // 100 * 10
num1 = b - b // 10 * 10
print(str(num1)+str(num2)+str(num3)+str(num4))

在这里插入图片描述

十、用户输入两个数a、b。如果a能被b整除或a加b大于1000,则输出a;否则输出b。

a=int(input("请输入数字a:"))
b=int(input("请输入数字b:"))
while True:
    if a/b or a+b>1000:
        print("输出的值为:%s"%a)
        break
    else:
        print("输出的值为:%s"%b)

在这里插入图片描述

十一、输入赵本山的考试成绩,显示所获奖励成绩==100分,爸爸给他买辆车成绩>=90分,妈妈给他买MP4,90分>成绩>=60分,妈妈给他买本参考书,成绩<60分,什么都不买

grade=int(input("请输入成绩:"))
if grade>100 or grade<0 :
              print("赵同学,不要开玩笑!!")
elif grade==100:
              print('赵同学,你爸爸要给你买车啦!!')
elif   grade>=90:
              print('赵同学,你妈妈要给你买MP4啦!!')
elif   grade<90 and grade>=60:
              print('赵同学,你妈妈要给你买本参考书啦!!')
elif  grade<60 and grade>=0:
              print('想啥啦,回去准备挨打吧!!')
else:
       print("不要调皮啦,请输入正确成绩!!")

在这里插入图片描述

十二、请输入一个数,判断这个数是偶数还是奇数,如果使用偶数,请判断从1到该数是3的倍数有哪些,如果是奇数,请判断从1到该数是5的倍数有哪些

num = int(input("请输入数字:"))
if num%2==0 :
    print('该数是偶数!!')
    for num in range(1,num+1):
      if num %3 ==0:
        print("该数从1到该数是3的倍数有:%s"%num)
else:
    print("该数是奇数!!")
    for num in range(1,num+1):
      if num % 5==0:
        print("该数从1到该数是5的倍数有:%s"%num)

在这里插入图片描述

十三、输入三边的长度,求三角形的面积和周长(海伦公式)

from cmath import sqrt
import math
a=float(input("请输入边长a:"))
b=float(input("请输入边长b:"))
c=float(input("请输入边长c:"))
if a+b>c and a+c>b and b+c>a:
  print("该三角形是三角形")
  p=(a+b+c)/2
  s1=p*(p-a)*(p-b)*(p-c)
  s=math.sqrt(s1)
  print('面积:'+str(s))
  print('周长:' + str(2*p))
else:
    print("该三角形不是三角形!!")

在这里插入图片描述

十四、某商店T恤的价格为35元/件(2件9折,3件以上8折),裤子的价格为120 元/条(2条以上9折).小明在该店买了3件T恤和2条裤子,请计算并显示小明应该付多少钱?

from secrets import choice
c_money=35
t_money=120
while True:
        num=int(input("请输入您需要商品的总数:"))
        clothes=int(input("请输入您需要衣服的件数:"))
        trousers=int(input("请输入您需要裤子的件数:"))
        if  num==1 :
            if clothes==1 or trousers==0:
                   print("您需要支付:衣服:%s"%(c_money)*clothes+"元,"+"裤子:%s"%(t_money)*trousers+"元,"+"您需要支付的总金额为:%s"%(c_money*clothes+t_money*trousers))
            elif clothes==0 or trousers==1:
                   print("您需要支付:衣服:%s"%(c_money)*clothes+"元,"+"裤子:%s"%(t_money)*trousers+"元,"+"您需要支付的总金额为:%s"%(c_money*clothes+t_money*trousers))
                   continue
        elif num==2:
            if clothes==0 or trousers==2:
              print("您需要支付:衣服:%s"%((c_money)*clothes*9/10)+"元,"+"裤子:%s"%((t_money)*trousers*9/10)+"元,"+"您需要支付的总金额为:%s"%((c_money*clothes+t_money*trousers)*9/10))
            elif clothes==1 or trousers==1:
                 print("您需要支付:衣服:%s"%(c_money)*clothes+"元,"+"裤子:%s"%(t_money)*trousers+"元,"+"您需要支付的总金额为:%s"%(c_money*clothes+t_money*trousers))
            elif clothes==2 or trousers==0:
               print("您需要支付:衣服:%s"%((c_money)*clothes*9/10)+"元,"+"裤子:%s"%((t_money)*trousers*9/10)+"元,"+"您需要支付的总金额为:%s"%((c_money*clothes+t_money*trousers)*9/10))   
               continue    
        elif num==3:
            if clothes==0 or trousers==3:
               print("您需要支付:衣服:%s"%((c_money)*clothes*9/10)+"元,"+"裤子:%s"%((t_money)*trousers*9/10)+"元,"+"您需要支付的总金额为:%s"%((c_money*clothes+t_money*trousers)*9/10)) 
            elif clothes==1 or trousers==2:
                 print("您需要支付:衣服:%s"%((c_money*clothes))+"元,"+"裤子:%s"%((t_money)*trousers*9/10)+"元,"+"您需要支付的总金额为:%s"%((c_money*clothes+t_money*trousers)*9/10))
            elif clothes==2 or trousers==1:
                print("您需要支付:衣服:%s"%((c_money)*clothes*9/10)+"元,"+"裤子:%s"%(t_money)*trousers+"元,"+"您需要支付的总金额为:%s"%((c_money*clothes*9/10)+t_money*trousers))
            elif clothes==3 or trousers==0:
                print("您需要支付:衣服:%s"%((c_money)*clothes*8/10)+"元,"+"裤子:%s"%((t_money)*trousers*9/10)+"元,"+"您需要支付的总金额为:%s"%((c_money*clothes*8/10)+t_money*trousers))
                continue
        elif num>=4:
                #  if clothes==1 or trousers>=2:
                #       print("您需要支付:衣服:%s"%((c_money)*clothes)+"元,"+"裤子:%s"%((t_money)*trousers*9/10)+"元,"+"您需要支付的总金额为:%s"%((c_money*clothes)+t_money*trousers*9/10)) 
                 if clothes==2 or trousers==2:
                      print("您需要支付:衣服:%s"%((c_money)*clothes*8/10)+"元,"+"裤子:%s"%((t_money)*trousers*9/10)+"元,"+"您需要支付的总金额为:%s"%((c_money*clothes*8/10)+(t_money*trousers*9/10)))
                 elif clothes>2 or trousers>2:   
                      print("您需要支付:衣服:%s"%((c_money)*clothes*8/10)+"元,"+"裤子:%s"%((t_money)*trousers*9/10)+"元,"+"您需要支付的总金额为:%s"%((c_money*clothes*8/10)+(t_money*trousers*9/10)))
        else:
            print("您输入有误")

在这里插入图片描述

十五、鸡兔同笼,从上面看有35个头,从下面看有94只脚,请问鸡有几只,兔有几只?

for x in range(1,36):
    for y in range(1,36):
       if x+y==35 and 2*x+4*y==94:
         print ("鸡有:%s"%x+"个")
         print("兔有:%s"%y+"个")


在这里插入图片描述

十六、猜数字游戏(使用random模块完成)电脑随机一个范围内的数,用户输入数据判断,如果数大了,提供”数大了“,成功之后,加上用户是否继续功能

import random
# 生成随机数
computer = random.randint(1, 100)
while True:
	my = int(input("请输入您要猜的数:"))

	if my == computer:
		print("恭喜您,猜对了")
		choice = input("您是否继续?结束请输入(n/N),按任意键继续:")
		if choice == "N" or choice == "n":
			break
		else:
			# 重新生成随机数
			computer = random.randint(1, 100)
	elif my > computer:
		print("对不起,猜大了")
	else:
		print("对不起,猜小了")

print("GAME OVER!!!")

在这里插入图片描述

十七、猜拳游戏:石头、剪刀、布的游戏

import random
# 生成随机数
computer = random.randint(1, 3)
while True:
	my = int(input("请输入以下数字:1代表石头、2代表剪刀、3代表布:"))
	if my == computer :
		print("咱们成平局了,电脑的随机数为:%s"%computer)
		choice = input("您是否继续?结束请输入(n/N),按任意键继续:")
		if choice == "N" or choice == "n":
			break
		else:
			computer = random.randint(1,3)
	elif 4>my>computer:
		print("恭喜,您赢了!!电脑的随机数为:%s"%computer)
	elif my>=4 or my<0:
		print("输入错误,请输入在1~3的数字!!!")
print("GAME OVER!!!")

在这里插入图片描述

十八、在控制台上,输入如下信息(使用循环)

a		a^2		a^3
1		1		1
2		4		8
3		9		27
# 方法一;
for a in range(1,4):
    print("a的值、平方、立方分别为:%s,%s,%s"%(a,a*a,a*a*a))

# 方法二:
 a=int(input("请输入您想要输入的数字:"))
 ji=0
 while True:
     print("a的值、平方、立方分别为:%s,%s,%s"%(a,a*a,a*a*a))
     ji=ji+1
     break

在这里插入图片描述

十九、输入一个年份,判断该年是否是闰年

year=int(input("请输入年份:"))
if (year % 4==0 and year % 100!=0) or year % 400==0:
  print("该年份是闰年")
else:
  print("该年不是闰年")

在这里插入图片描述

  • 8
    点赞
  • 115
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
一种使用for循环Python题目是计算给定数字的平方和立方。可以使用以下代码实现: ``` for a in range(1, 4): print("a的值、平方、立方分别为:%s,%s,%s" % (a, a*a, a*a*a)) ``` 这个代码将输出1到3之间的数字的平方和立方。 另一个小题目是输入三角形的三边长度,然后使用海伦公式计算三角形的面积和周长。可以使用以下代码实现: ``` from cmath import sqrt import math a = float(input("请输入边长a:")) b = float(input("请输入边长b:")) c = float(input("请输入边长c:")) if a+b > c and a+c > b and b+c > a: print("该三角形是三角形") p = (a+b+c) / 2 s1 = p*(p-a)*(p-b)*(p-c) s = math.sqrt(s1) print('面积:' + str(s)) print('周长:' + str(2*p)) else: print("该三角形不是三角形!!") ``` 这个代码将根据输入的三边长度判断并计算三角形的面积和周长,使用了海伦公式进行计算。 另一个小题目是使用嵌套的for循环输出一个数字图案。可以使用以下代码实现: ``` a = 0 b = 1 for i in range(1, 5): for j in range(a, b): print(j, end='') print() a = a + i b = b + i ``` 这个代码将输出数字图案:0、12、345、6789。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [Python循环题目练习(for、while、if)](https://blog.csdn.net/qq_50589028/article/details/123829530)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *3* [python 双重for循环输出题目](https://blog.csdn.net/weixin_42249184/article/details/119809798)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值