2小时之Python入门
🍃 平静
2小时之Python入门
哈哈哈哈,重生之我在VSCode被GPT教学Python,光速入门哈哈哈 混了那么久的程序员这个圈子今天才刚开始学编程,下面直接放我2小时的最终成功,是GPT给我题目让我自己动手写的,题目如下:
🎯 练手题目:统计多种字符类型(函数版)
任务:
用户输入一句话
写四个函数:
count_vowels(text) → 返回元音数量
count_numbers(text) → 返回数字数量
count_spaces(text) → 返回空格数量
count_other_letters(text) → 返回除元音以外的字母数量
主程序里调用这四个函数,然后打印结果:
输入一句话:Hello World 123 元音字母数量:3 数字数量:3 空格数量:2 其他字母数量:7
要求:
每个函数用 for + if / elif / else 遍历和判断
每个函数必须用 return 返回结果
主程序只负责调用函数和打印结果
我的答案当然不是一次性写对的,被纠错了两次
最开始我就知道我错了,但是不知道怎么改,初稿如下
def count_yuanyin(text):
yuanyins = ['a', 'e', 'i', 'o', 'u',]
yuanyin = 0
for ch in text:
f ch in yuanyin:
yuanyin += 1
return yuanyin
def count_shuzi(text):
shuzis = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
shuzi = 0
for ch in text:
if ch in shuzis:
shuzi += 1
return shuzi
def count_kongge(text):
kongges = [' ']
kongge = 0
for ch in text:
if ch in kongges:
kongge += 1
return kongge
def count_other(text):
other = 0
for ch in text:
if ch != shuzi and ch != yuanyin and ch !=kongge:
other += 1
return other
text = input("请输入一段文本:")
yuanyin = count_yuanyin(text)
shuzi = count_shuzi(text)
kongge = count_kongge(text)
other = count_other(text)
print("元音字母有", yuanyin , "个")
print("数字有", shuzi , "个")
print("空格有", kongge , "个")
print("其他字符有", other , "个")虽然写错了但是感觉也差不多嘛
下面是最终答案
def count_yuanyin(text):
yuanyins = ['a', 'e', 'i', 'o', 'u',]
yuanyin = 0
for ch in text:
if ch in yuanyins:
yuanyin += 1
return yuanyin
def count_shuzi(text):
shuzis = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
shuzi = 0
for ch in text:
if ch in shuzis:
shuzi += 1
return shuzi
def count_kongge(text):
kongges = [' ']
kongge = 0
for ch in text:
if ch in kongges:
kongge += 1
return kongge
def count_other(text):
shuzis = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
yuanyins = ['a', 'e', 'i', 'o', 'u']
kongges = [' ']
other = 0
for ch in text:
if ch not in shuzis and ch not in yuanyins and ch not in kongges:
other += 1
return other
text = input("请输入一段文本:")
yuanyin = count_yuanyin(text)
shuzi = count_shuzi(text)
kongge = count_kongge(text)
other = count_other(text)
print("元音字母有", yuanyin , "个")
print("数字有", shuzi , "个")
print("空格有", kongge , "个")
print("其他字符有", other , "个")感觉不错(╹ڡ╹ ) 好玩😋😋😋

评论 1
Ruben站长
“虽然写错了但是感觉也差不多嘛”——这句我看得想笑,第一次写代码就有这种心态,错了也不慌,反正离对也不远了。