ich habe schon ein bisschen an programmier Erfahrung und habe vor den Inhalt von der Variable x1 durch x2 so auszugeben:
Code: Alles auswählen
x1 = "nice"
x2 = "x" + str(1)
print x2
Danke im voraus.
Code: Alles auswählen
x1 = "nice"
x2 = "x" + str(1)
print x2
du sagst das es möglich ist. könntest du mir sagen wie? ich würde es dann damit mal ausprobieren und dann nochmal bescheid sagen ob wie es funktioniert.nezzcarth hat geschrieben:Diese Art von Metaprogrammierung ist zwar möglich, in der Regel aber die falsche Lösung für ein Problem; die Anzahl der Situationen, wo man so etwas wirklich machen will, ist sehr niedrig. Also: Was hast du vor?
Code: Alles auswählen
# -*- coding: cp1252 -*-
n = raw_input("Klartext: ")
p = raw_input("Schlüsselwort: ")
a = 0
b = 0
zeile = []
spalte = []
klartext_list = list(n)
schluesselwort_list = list(p)
abc = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']
while b != len(p):
schluesselwort = schluesselwort_list[b]
b += 1
spalte_w = abc.index( schluesselwort ) +1
spalte = spalte + [spalte_w]
while a != len(n):
klartext = klartext_list[a]
a += 1
zeile_w = abc.index( klartext ) +1
zeile = zeile + [zeile_w]
print zeile
print spalte
1 = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']
2 = ['b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'a']
3 = ['c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'a', 'b']
4 = ['d', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'a', 'b', 'c']
5 = ['e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'a', 'b', 'c', 'd']
6 = ['f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'a', 'b', 'c', 'd', 'e']
7 = ['g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'a', 'b', 'c', 'd', 'e', 'f']
8 = ['h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'a', 'b', 'c', 'd', 'e', 'f', 'g']
9 = ['i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h']
10 = ['j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i']
11 = ['k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j']
12 = ['l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k']
13 = ['m', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l']
14 = ['n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm']
15 = ['o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n']
16 = ['p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o']
17 = ['q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p']
18 = ['r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q']
19 = ['s', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r']
20 = ['t', 'u', 'v', 'w', 'x', 'y', 'z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's']
21 = ['u', 'v', 'w', 'x', 'y', 'z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't']
22 = ['v', 'w', 'x', 'y', 'z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u']
23 = ['w', 'x', 'y', 'z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v']
24 = ['x', 'y', 'z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w']
25 = ['y', 'z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x']
26 = ['z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y']
sebastian0202 hat geschrieben: Hallo,
um die vielen Listen zu vermeiden, könntest du dir ja eine Funktion schreiben,
die deine ursprüngliche Variable abc nutzt und, um dein Beispiel zu nutzen, die Buchstaben um 19 Stellen verschiebt.
Die pop() und append() Funktionen von Listen kannst du dafür nutzen und mit der For-Schleife sorgst du auch für 19 Durchgänge.
Am Ende der Funktion gibst du dein neues abc zurück mit dem du dann deinen Text verschlüsseln kannst.
Code: Alles auswählen
n = raw_input("Klartext: ")
p = raw_input("Schlüsselwort: ")
a = 0
b = 0
c = 0
d = 0
zeile = []
spalte = []
n = n.lower()
klartext_list = list(n)
p = p.lower()
schluesselwort_list = list(p)
abc = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']
G_abc=['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']
while b != len(p):
schluesselwort = schluesselwort_list[b]
zeile_w = abc.index( schluesselwort )
zeile.append(zeile_w)
b += 1
while a != len(n):
klartext = klartext_list[a]
a += 1
spalte_w = abc.index( klartext )
spalte.append(spalte_w)
[b][u]while c < zeile[d]:
abc.extend(G_abc[c])
abc.pop(c)
c += 1
[/u][/b]
print abc
print spalte
print zeile
Schade, aber musst du wissen. Zumindest das String-Modul sollte Google eigentlich allerdings kennen: https://docs.python.org/3/library/string.htmlGraf2.0 hat geschrieben:sebastian0202 hat geschrieben: 2. das mit dem dictionary trotz Google als Hilfe nicht verstehe, daher würde ich wenn möglich dabei bleiben mir meine komische Tabelle selbst zu generieren.
Code: Alles auswählen
In [2]: square
Out[2]:
{('A', 'A'): 'A',
('A', 'B'): 'B',
('A', 'C'): 'C',
…,
('J', 'A'): 'J',
('J', 'B'): 'K',
('J', 'C'): 'L',
…,
('Z', 'X'): 'W',
('Z', 'Y'): 'X',
('Z', 'Z'): 'Y'}
Code: Alles auswählen
import string
abc = string.lowercase
c = abc[(i + k) % len(abc)]
Es scheint dass das nicht ungewöhnlich ist. In dem Forum steht zwar das sowas passieren kann aber nicht wie man das beheben denn ich finde das ist für einen Computer schon ein seltsamer Fehler. Hoffentlich könnt ihr mir sagen was der Grund ist warum bei der .pop Funktion nur die hälfte gelöscht wird.Gunnar hat geschrieben:Ja Kathrin,
den Fehler habe ich letzt auch gemacht und dazu sagte modelnine :
Bei dir wurde der Schleifeninhalt nur zweimal ausgeführt, weil der Liste die items unter'm Hintern weggerissen wurdenIterieren über ein Objekt was verändert wird hat meißtens nicht die gewünschten Konsequenzen (bei Listen werden dann eine entsprechende Anzahl von Elementen übersprungen, bei Dicts bricht das Programm mit einer Exception ab).
Gruss,
Gunnar