私信  •  关注

FishingCode

FishingCode 最近创建的主题
FishingCode 最近回复了
4 年前
回复了 FishingCode 创建的主题 » 如何在python中通过逐字打印和反向打印字符串

使用用户输入进行此操作的一个简单方法应该是:

newstring = "" 
enterString = (str(input("Enter a string to be reversed:")))
count = 0
for i in reversed(enterString): 
   newstring += i
   count += 1
   print ("Reversed string %s is this: %s" % (count, newstring))

输出,计数直到最后一个字符的次数:

 Enter a string to be reversed:hello
 Reversed string 1 is this: o
 Reversed string 2 is this: ol
 Reversed string 3 is this: oll
 Reversed string 4 is this: olle
 Reversed string 5 is this: olleh
4 年前
回复了 FishingCode 创建的主题 » 在python混乱中分配变量[关闭]

所以如果m=5,那么m变成6:

m += 1 

意味着您将m递增1(后递增)

m = m + 1

不能分配:

1 += m

SyntaxError: can't assign to literal
4 年前
回复了 FishingCode 创建的主题 » 将字符串转换为数组中的python

我不知道你到底在问什么,但如果你想找到“2”所在的索引,你可以这样做,否则请详细说明:

storeString = [["","","",""],["","2","",""],["","","",""],["2","","",""]]

for i in storeString:
   if "2" in i:
     print(i.index("2"))
4 年前
回复了 FishingCode 创建的主题 » 尝试对用户输入使用词典(python)

你也可以这样做:

 hero_choices = { 'batman': 'Moon Knight',
                  'moon knight: 'Batman',
                  'superman':'Hyperion',
                  'hyperion': 'Superman',
                   ...
                }

 getChoice = str(input("Please choose a hero:\n"))


 for key, value in hero_choices.items():
    if key == "getChoice":
       print(hero_choices[key])
    elif key != "getChoice":
       print("This hero doesn't exist!")

这是上述方案的替代方案。

4 年前
回复了 FishingCode 创建的主题 » 如何通过Python打开命令提示符[duplicate]

在unix系统上,可以使用shebang(!#)在你的蟒蛇3开始处排队 脚本:

 #!/usr/bin/env python3 

 chmod +x ./the/pathtofilename