python基础题目(python基础题求思路!!)

本文目录
python基础题求思路!!
Times = raw_input(u“请输入格式为HHMMSS的时间,表示时分秒:“)
slen = len(Times)
if slen 《 6:
print u“小时错误,分钟错误,秒错误“
else:
hour = Times[0:2]
minute = Times[2:4]
second = Times[4:]
if hour.isdigit() and minute.isdigit() and second.isdigit():
if int(hour) 》 24:
print u“小时错误“,
elif int(minute) 》 60:
print u“分钟错误“,
elif int(second) 》 60:
print u“秒错误“
else:
print u“{0}时{1}分{2}秒“.format(hour, minute, second)
else:
print u“小时错误,分钟错误,秒错误“
Python 基础题急求!
def ingredient(recipes, menu):
....res =
....for x in range(1,len(recipes)):
........res.append([recipes[x],0])
........for y in range(1,len(recipes)):
............if recipes[y] in my_menu:
................res[x-1]+=recipes[x][y]
....return res
大佬,能帮我看看这两个Python基础题吗
第一道题,可能写的有点麻烦,可以简化。: /
import random
#获取随机数并写入
for i in range(10):
a = str(random.randrange(1,100))
file = open(“file.txt“,“a+“)
file.write(a)
file.write(“ “)
#写入结束
#读取
file = open(“file.txt“,’r’)
number = file.read()
nb = number.split()
a = 0
list =
for i in range(10):
number = int(nb[a])
a += 1
list.insert(0,number)
list.sort()
b = 0
for i in range(10):
print(list[b],end = “ “)
b += 1
file.close()
我把他转成列表排序再输出了。
Python中基础练习题
法一:利用set()函数的去重功能,去重后再使用list()函数将集合转换为我们想要的列表
list1 = [11,22,33]
list2 = [22,33,44]
list3 = list(set(list1 + list2))
list3.sort()
print(list3)
-------------
法二:利用if和for,先遍历list1所有元素追加到list3中,然后遍历list2,条件判断list2中当前元素是否在list3中,如果不在则追加到list3中
list1 = [11,22,33]
list2 = [22,33,44]
list3 =
for ele1 in list1:
list3.append(ele1)
for ele2 in list2:
if ele2 not in list3:
list3.append(ele2)
print(list3)
python基础题
(1)count = 0
(2)while count 《 3:
(3) name = input()
(4) password = input()
(5) if name == ’Kate’ and password == ’666666’:
(6) print(“登录成功!“)
(7) break
(8) else:
(9) count += 1
(10) if count == 3:
(11) print(“3次用户名或者密码均有误!退出程序!“)
程序开始执行:
(1):定义int类型变量count并为其赋初始值0,执行语句(2)。
(2):循环语句,若变量count》=3则跳出循环,程序结束。若count《3则进入循环,执行语句(3)。
(3):定义str类型变量name并调用python内置输入函数input(),控制台等待输入,假设输入“Kate“,执行语句(4)。
(4):定义str类型变量password并调用python内置输入函数input(),控制台等待输入,假设输入“666666“。执行语句(5)
(5):判断语句,若name变量的__str__()函数的返回值等于字符串’Kate’的__str__()函数的返回值且password变量__str__()函数的返回值等于字符串’666666’的__str__()函数的返回值则执行语句(6),否则执行语句(9),因假设中name变量的值为“Kate“,password变量的值为“666666“,故执行语句(6)
(6):调用内置输出函数print(self, *args, sep=’ ’, end=’\n’, file=None),其中*args对应实参为“登录成功!”,故输出“登录成功”。执行语句(7)
(7):break关键字,跳出循环,程序无后续代码,程序结束。
(9):count变量的值等于count变量的值加1。执行语句(10)
(10):判断count变量的值是否等于3,如果是执行语句(11),否则执行语句(2)
(11):调用内置输出函数print(self, *args, sep=’ ’, end=’\n’, file=None),其中*args对应实参为“3次用户名或密码均有误!退出程序”,故输出“3次用户名或密码均有误!退出程序”。执行语句(2),因count》=3,故执行完(2)后程序结束。
Python基础作业有四道题不会
1、元组是不能排序,但他也没有让你排序呀!简单说就是直接把元组转成列表然后进行排序并输出就好了。方法很多,多做尝试。
2、简单编写:
n1 = input(’输入一个整数:’)
n2 = input(’输入一个整数:’)
print(n1+n2)
(思路:在键盘获取两个整数,注意一定要规定数据类型。将获取来的值赋给变量,在对变量进行加操作并打印就成了。)
3、简答编写(程序并不完成,学习自己钻研):
class Person:
……
def say_hello(self):
print(“hello!“)

更多文章:
another time(another time和other time的区别)
2026年10月11日 05:00







