I am working toward converting the logic programmed in Python to PyTeal or Algorand Python. The program is a simple game, where the user pays a specified amount x
in the example below 7
. If the payment amount is correct, the game generates a random number from 0 to 9. If the number is 7 the user wins 70, else the user wins 0.
I’m consistently running into errors with the PyTeal logic. I’m looking for a base description of the necessary elements of the language, so I can re-write my python script for processing on Algorand. Any suggestions or guidance would be appreciated.
Python Code
import random
# x is the payment recieved by the smart contract
x = 7
# if the amont recieved is equal to 7, the following logic proceeds
if x == 7:
# one random numbers are generated
h = random.randint(0,9)
print(h)
# Scoring
a = 0
if h == 7:
print('reward = 70')
else:
print('better luck next time')
PyTeal
from pyteal import *
# Check if the payment received is equal to 7
Txn.amount() == int(7),
# Generate a pseudo-random number using the round number as a seed
int(0) <= Global.round() % int(10) <= int(9),
if Global.round() % Int(10) == Int(7):
App.localPut(Int(0), Bytes("h"), int(70))
else:
App.localPut(Int(0), Bytes("h"), int(0))
# Print the compiled TEAL program
print(compileTeal(program, mode=Mode.Application))
Error
Traceback (most recent call last):
File "/Users/brianhaney/Desktop/game2.py", line 6, in <module>
Txn.amount() == int(7),
^^^^^^^^^^^^^^^^^^^^^^
File "/Users/brianhaney/myenv/lib/python3.12/site-packages/pyteal/ast/expr.py", line 67, in __eq__
return Eq(self, other)
^^^^^^^^^^^^^^^
File "/Users/brianhaney/myenv/lib/python3.12/site-packages/pyteal/ast/binaryexpr.py", line 186, in Eq
return BinaryExpr(Op.eq, right.type_of(), TealType.uint64, left, right)
^^^^^^^^^^^^^
AttributeError: 'int' object has no attribute 'type_of'